﻿<!DOCTYPE html>
<html lang="en" dir="ltr" xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <meta name="viewport" content="user-scalable=1.0,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="robots" content="noindex,nofollow">
    <link rel="manifest" href="{{{domainurl}}}manifest.json">
    <link rel="shortcut icon" href="{{{domainurl}}}favicon.ico" />
    <link rel="icon" type="image/png" sizes="16x16" href="{{{domainurl}}}favicon-16x16.png">
    <link rel="icon" type="image/png" sizes="32x32" href="{{{domainurl}}}favicon-32x32.png">
    <link rel="apple-touch-icon" href="/favicon-303x303.png" />
    <link type="text/css" href="styles/xterm.css" media="screen" rel="stylesheet" title="CSS" />
    {{{customCSSTags}}}
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="#ffffff">
    <meta name="apple-mobile-web-app-title" content="{{{title}}}">
    <script type="text/javascript" src="scripts/common-0.0.1{{{min}}}.js"></script>
    <script type="text/javascript" src="scripts/meshcentral{{{min}}}.js"></script>
    <script type="text/javascript" src="scripts/agent-redir-ws-0.1.1{{{min}}}.js"></script>
    <script type="text/javascript" src="scripts/agent-desktop-0.0.2{{{min}}}.js"></script>
    <script type="text/javascript" src="scripts/amt-0.2.0{{{min}}}.js"></script>
    <script type="text/javascript" src="scripts/amt-redir-ws-0.1.0{{{min}}}.js"></script>
    <script type="text/javascript" src="scripts/amt-desktop-0.0.2{{{min}}}.js"></script>
    <script type="text/javascript" src="scripts/xterm{{{min}}}.js"></script>
    <script type="text/javascript" src="scripts/xterm-addon-fit{{{min}}}.js"></script>
    <script type="text/javascript" src="scripts/zlib{{{min}}}.js"></script>
    <script type="text/javascript" src="scripts/zlib-inflate{{{min}}}.js"></script>
    <script type="text/javascript" src="scripts/zlib-adler32{{{min}}}.js"></script>
    <script type="text/javascript" src="scripts/zlib-crc32{{{min}}}.js"></script>
    <script keeplink=1 type="text/javascript" src="scripts/filesaver.min.js"></script>
    {{{customJSTags}}}
    <meta name="msapplication-TileColor" content="#00aba9">
    <meta name="theme-color" content="#ffffff">
    <title>{{{title}}}</title>
    <style>
        body {
            background-color: white;
        }

        .night body {
            background-color: black;
        }

        #MxMESH {
            color: black;
        }

        .night #MxMESH {
            color: lightgray;
        }

        .textOverGray { color: black; }

        #dialog {
            z-index:1000;
            background-color:#EEE;
            box-shadow:0px 0px 15px #666;
            font-family:Arial,Helvetica,sans-serif;
            border-radius:5px;
            position:fixed;
            top:90px;
            width:300px;
        }

        .night #dialog {
            color: black;
            background-color:#AAA;
        }

        :focus {
            outline: 0;
        }

        a {
            color: #036;
            text-decoration: underline;
        }

        .night a {
            color: #99F;
        }

        #footer a {
            color: #fff;
            text-decoration: underline;
        }

            #footer a:hover {
                text-decoration: none;
            }

        .night #footer {
            color: gray;
        }

        .i1 {
            background: url(../images/icons50.png) 0px 0px;
            background-image: image-set(url(../images/icons50.png) 1x, url(../images/icons100.png) 2x);
            height: 50px;
            width: 50px;
            border: none;
        }

        .i2 {
            background: url(../images/icons50.png) -50px 0px;
            background-image: image-set(url(../images/icons50.png) 1x, url(../images/icons100.png) 2x);
            height: 50px;
            width: 50px;
            border: none;
        }

        .i3 {
            background: url(../images/icons50.png) -100px 0px;
            background-image: image-set(url(../images/icons50.png) 1x, url(../images/icons100.png) 2x);
            height: 50px;
            width: 50px;
            border: none;
        }

        .i4 {
            background: url(../images/icons50.png) -150px 0px;
            background-image: image-set(url(../images/icons50.png) 1x, url(../images/icons100.png) 2x);
            height: 50px;
            width: 50px;
            border: none;
        }

        .i5 {
            background: url(../images/icons50.png) -200px 0px;
            background-image: image-set(url(../images/icons50.png) 1x, url(../images/icons100.png) 2x);
            height: 50px;
            width: 50px;
            border: none;
        }

        .i6 {
            background: url(../images/icons50.png) -250px 0px;
            background-image: image-set(url(../images/icons50.png) 1x, url(../images/icons100.png) 2x);
            height: 50px;
            width: 50px;
            border: none;
        }

        .i7 {
            background: url(../images/icons50.png) -300px 0px;
            background-image: image-set(url(../images/icons50.png) 1x, url(../images/icons100.png) 2x);
            height: 50px;
            width: 50px;
            border: none;
        }

        .i8 {
            background: url(../images/icons50.png) -350px 0px;
            background-image: image-set(url(../images/icons50.png) 1x, url(../images/icons100.png) 2x);
            height: 50px;
            width: 50px;
            border: none;
        }

        .m0 {
            background: url(../images/images16.png) -32px 0px;
            height: 16px;
            width: 16px;
            border: none;
            float: left;
        }

        .m1 {
            background: url(../images/images16.png) -16px 0px;
            height: 16px;
            width: 16px;
            border: none;
            float: left;
        }

        .m2 {
            background: url(../images/images16.png) -96px 0px;
            height: 16px;
            width: 16px;
            border: none;
            float: left;
        }

        .m3 {
            background: url(../images/images16.png) -112px 0px;
            height: 16px;
            width: 16px;
            border: none;
            float: left;
        }

        .m4 {
            background: url(../images/images16.png) -128px 0px;
            height: 16px;
            width: 16px;
            border: none;
            float: left;
        }

        .gray {
            /*filter: url("data:image/svg+xml;utf8,&lt;svg xmlns=\'http://www.w3.org/2000/svg\'&gt;&lt;filter id=\'grayscale\'&gt;&lt;feColorMatrix type=\'matrix\' values=\'0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0.3333 0.3333 0.3333 0 0 0 0 0 1 0\'/&gt;&lt;/filter&gt;&lt;/svg&gt;#grayscale");*/ /* Firefox 10+, Firefox on Android */
            filter: gray; /* IE6-9 */
            -webkit-filter: grayscale(100%) opacity(60%); /* Chrome 19+, Safari 6+, Safari 6+ iOS */
        }

        .DevSt {
            padding-left: 5px;
            border-bottom-style: solid;
            border-bottom-width: 1px;
            border-bottom-color: #DDDDDD;
        }

        .noselect {
            -webkit-touch-callout: none;
            -webkit-user-select: none;
            -khtml-user-select: none;
            -moz-user-select: none;
            -ms-user-select: none;
            user-select: none;
        }

        .fileIcon1 {
            background: url();
            height: 16px;
            width: 16px;
            cursor: pointer;
            border: none;
            float: left;
            margin-top: 1px;
        }

        .fileIcon2 {
            background: url();
            height: 16px;
            width: 16px;
            cursor: pointer;
            border: none;
            float: left;
            margin-top: 1px;
        }

        .fileIcon3 {
            background: url();
            height: 16px;
            width: 16px;
            cursor: pointer;
            border: none;
            float: left;
            margin-top: 1px;
        }

        .fileIcon4 {
            background: url(../images/meshicon16.png);
            height: 16px;
            width: 16px;
            cursor: pointer;
            border: none;
            float: left;
            margin-top: 1px;
        }

        .filelist {
            -moz-user-select: none;
            -khtml-user-select: none;
            -webkit-user-select: none;
            -o-user-select: none;
            cursor: default;
            -khtml-user-drag: element;
            clear: both;
        }

        .style10 {
            background-color: #C9C9C9;
            color: #000;
        }

        .night .style10 {
            background-color: #888;
        }

        .meshList {
            width:auto;
            height:40px;
            background-color:lightgray;
            margin-top:5px;
            margin-bottom:5px;
            margin-left:60px;
            padding-top:5px;
            padding-bottom:5px;
            border-radius:8px 0px 0px 8px;
        }

        .night .meshList {
            background-color: gray;
        }

        .night .devList3 {
            background-color: gray;
        }

        .devList4 {
            padding-left: 12px;
            padding-top: 2px;
            color: black;
        }

        .devList5 {
            padding-left: 12px;
            padding-top: 3px;
            color: #444
        }

        .night .devList5 {
            color: black;
        }

        .deskButton {
            box-shadow: 0px 0px 10px #000;
            border-radius:20px;
            position:absolute;
            right:10px;
            top:10px;
            cursor:pointer;
            background-color:#AAA;
            z-index:1000;
        }

        .menuButton{
            box-shadow: 0px 0px 10px #000;
            border-radius:10px;
            display:inline-block;
            width:120px;
            background-color:#AAA;
            text-align:center;
            padding:8px;
            cursor:pointer;
            margin:10px;
            z-index:1000;
        }

        #p15statetext {
            padding: 4px;
            height: 15px;
        }

        #p15agentConsole {
            background: black;
            margin: 0;
            padding: 0;
            color: lightgray;
            width: 100%;
            position: relative;
        }

        #p15coreName {
            padding: 4px;
            display: inline-block;
        }

        #p15agentConsoleText {
            position:absolute;
            margin: 0;
            padding: 0;
            top: 0;
            bottom: 0;
            left:0;
            right: 0;
            overflow-y: scroll;
            overflow-x: auto;
        }

        .areaHead {
            padding-top: 2px;
            padding-bottom: 2px;
            background: #C0C0C0;
        }

        .night .areaHead {
            color: #CCC;
            background: #333;
        }

        .areaFoot {
            padding-top: 2px;
            padding-bottom: 2px;
            background: #C0C0C0;
        }

        .night .areaFoot {
            color: #CCC;
            background: #333;
        }

        .toright2 {
            float: right;
            text-align: right;
        }

        #consoleTable {
            width: 100%;
            height: 100%;
            padding: 0px;
            margin-top: 0px;
        }

        .night #consoleTable {
            color: black;
        }

        #termTable {
            width: 100%;
            padding: 0px;
            margin-top: 0px;
        }

        .fulldesk #termTable {
            position: absolute;
            top: 0;
            bottom: 0;
            left: 0;
            right: 0;
        }

        #termarea3x {
            background: black;
            text-align: center;
            height: 400px;
            position: relative;
        }
    </style>
</head>
<body id="body" onload="if (typeof(startup) !== 'undefined') startup();" style="overflow-y:hidden;margin:0;padding:0;border:0;font-size:13px;font-family:\'Trebuchet MS\', Arial, Helvetica, sans-serif">
    <div id=container>
        <div id="notifiyBox" class="notifiyBox" style="display:none"></div>
        <div id=mastheadx></div>
        <div id=masthead style="background:url(logo.png) 0px 0px;background-size:341px 50px;background-color:#036;background-repeat:no-repeat;height:50px;width:100%;overflow:hidden">
            <div style="width:calc(100% - 50px);overflow:hidden">
                <div style="float:left;height:66px;color:#c8c8c8;padding-left:10px;padding-top:6px">
                    <strong><font style="font-size:36px;font-family:Arial,Helvetica,sans-serif">{{{title1}}}</font></strong>
                </div>
                <div style="float:left;height:66px;color:#c8c8c8;padding-left:5px;padding-top:10px">
                    <strong><font style="font-size:12px;font-family:Arial,Helvetica,sans-serif">{{{title2}}}</font></strong>
                </div>
            </div>
        </div>
        <div id=page_content style="position:absolute;bottom:32px;top:50px;width:100%">
            <div id=column_l style="width:100%;padding:0;position:absolute;bottom:0px;top:0px">
                <div id=p0 style=display:none;width:100%;height:100%>
                    <div style="display:flex;align-items:center;width:100%;height:100%">
                        <div id=p0message style=text-align:center;width:100%><span id="p0span">Server disconnected</span>, <href onclick=reload() style=cursor:pointer><u>click to reconnect</u></href>.</div>
                    </div>
                </div>
                <div id=p1 style=display:none;width:100%;height:100%>
                    <div style="display:flex;align-items:center;width:100%;height:100%">
                        <div id=p1message style=text-align:center;width:100%></div>
                    </div>
                </div>
                <div id=p10 style=display:none;position:absolute;bottom:0;top:0;width:100%;overflow:hidden>
                    <table id=p10deskTopTable cellspacing=0 style="margin:0;padding:0;border-spacing:0;border:0;position:absolute;top:0">
                        <tr style=padding:0>
                            <td>
                                <div style="margin-left:10px;margin-top:14px;font-size:20px">
                                    <strong><span id=p10deviceName></span></strong>
                                </div>
                            </td>
                        </tr>
                    </table>
                    <div id=p10dialog style="z-index:1000;background-color:#EEE;box-shadow:0px 0px 15px #666;font-family:Arial,Helvetica,sans-serif;border-radius:5px;position:fixed;top:30px;width:300px;left:30px;display:none">
                        <div style="width:100%;background-color:#003366;color:#FFF;border-radius:5px 5px 0 0">
                            <div style=padding:5px>Keyboard Shortcuts Customization</div>
                            <div style=width:100%;margin:6px></div>
                        </div>
                        <div style="margin-right:16px;margin-left:8px"><div id=p10dialog2 style="margin:auto;margin:3px"></div></div>
                        <div style="padding:10px;margin-bottom:20px"><input type="button" value="OK" style="float:right;width:80px" onclick="deskCustomizeKeysEx()"></div>
                    </div>
                    <img id="deskkeybutton1" src="images/mobile-desk-exit.png" class="deskButton" style="top:10px;display:none" onclick="exitButton()" />
                    <img id="deskkeybutton3a" src="images/mobile-desk-menu-open.png" class="deskButton" style="top:60px;display:none" onclick="toggleMenu(false)" />
                    <img id="deskkeybutton3b" src="images/mobile-desk-menu-close.png" class="deskButton" style="top:60px;display:none" onclick="toggleMenu(true)" />
                    <img id="deskkeybutton4a" src="images/mobile-desk-mouse-left.png" class="deskButton" style="top:110px;display:none" onclick="deskChangeMouseButton(0)" />
                    <img id="deskkeybutton4b" src="images/mobile-desk-mouse-right.png" class="deskButton" style="top:110px;display:none" onclick="deskChangeMouseButton(1)" />
                    <img id="deskkeybutton5a" src="images/mobile-desk-scale-out.png" class="deskButton" style="top:160px;display:none" onclick="deskChangeFullscreenZoom()" />
                    <img id="deskkeybutton5b" src="images/mobile-desk-scale-in.png" class="deskButton" style="top:160px;display:none" onclick="deskChangeFullscreenZoom()" />
                    <img id="deskkeybutton2a" src="images/mobile-desk-keyboard-open.png" class="deskButton" style="top:210px;display:none" onclick="toggleKeyboard()" />
                    <img id="deskkeybutton2b" src="images/mobile-desk-keyboard-close.png" class="deskButton" style="top:210px;display:none" onclick="toggleKeyboard()" />
                    <div style="position:absolute;top:0;left:0;z-index:200;opacity:0;width:1px;height:1px">
                        <input id="softKeyboard" autocapitalize="off" autocomplete="off" type="text" inputmode="text" spellcheck="false" style="z-index:200;opacity:0;width:1px;height:1px" onfocus="keyboardFocusChange()" onblur="keyboardFocusChange()" />
                    </div>
                    <div id="deskButtonMenu" style="display:none;position:absolute;top:10px;left:10px;right:55px;bottom:10px;z-index:1000"></div>
                    <div id=p10desktop style="overflow:hidden;position:absolute;top:55px;bottom:0px;width:100%;display:none">
                        <div id=deskarea1 style="position:absolute;top:0px;width:100%;height:32px">
                            <div style="padding-top:2px;padding-bottom:2px;background:#C0C0C0;height:32px">
                                <div style="float:right;text-align:right">
                                    <span id="p14power"></span>&nbsp;
                                    <input type=button id=deskFullScreen value="Full Screen" onclick=deskToggleFull(event) onkeypress="return false" onkeydown="return false" disabled="disabled" style="height:28px;margin-right:3px;">
                                </div>
                                <div style="margin-left:3px">
                                    <input type=button id=connectbutton1 value="Connect" onclick=connectDesktop(event,3) onkeypress="return false" onkeydown="return false" disabled="disabled" style="height:28px">
                                    <input type=button id=connectbutton1h value="HW Connect" onclick=connectDesktop(event,2) onkeypress="return false" onkeydown="return false" disabled="disabled" style="height:28px">
                                    <input type=button id=disconnectbutton1 value="Disconnect" onclick=connectDesktop(event,0) onkeypress="return false" onkeydown="return false" style="height:28px">
                                    <span id="deskstatus" style="color:black">Disconnected</span>
                                </div>
                            </div>
                        </div>
                        <div id=deskarea3 style="position:absolute;top:32px;width:100%;height:calc(100% - 64px);background-color:#000;text-align:center">
                            <div id=DeskParent style="height:100%">
                                <canvas id=Desk width=640 height=200 style="width:100%;-ms-touch-action:none;margin-left:0px" oncontextmenu="return false" onmousedown=dmousedown(event) onmouseup=dmouseup(event) onmousemove=dmousemove(event) onmousewheel=dmousewheel(event)></canvas>
                            </div>
                            <div id=p11DeskConsoleMsg style="display:none;cursor:pointer;position:absolute;left:30px;top:17px;color:yellow;background-color:rgba(0,0,0,0.6);padding:10px;border-radius:5px;text-align:left" onclick=p11clearConsoleMsg()></div>
                            <div id=p11DeskSessionSelector style="display:none;position:absolute;left:30px;top:17px;right:30px;bottom:17px;overflow-y:auto"></div>
                        </div>
                        <div id=deskarea4 style="position:absolute;bottom:0px;width:100%;height:32px">
                            <div style=padding-top:2px;padding-bottom:2px;background:#C0C0C0>
                                <div style=float:right;text-align:right>
                                    <!--<input id=DeskToolsButton type=button value=Tools onkeypress="return false" onkeydown="return false" onclick="toggleDeskTools()">&nbsp;-->
                                </div>
                                <div>
                                    <input type="button" value="Settings" onkeypress="return false" onkeydown="return false" onclick="showDesktopSettings()" style="height:28px">
                                    <input type="button" id="DeskScreens" value="Screens" onkeypress="return false" onkeydown="return false" onclick="deskSelectScreens()" style="display:none;height:28px">
                                    <label><span id="DeskControlSpan" style="display:none"><input id="DeskControl" type="checkbox" onkeypress="return false" onkeydown="return false">Input</span></label>
                                </div>
                            </div>
                        </div>
                    </div>
                    <div id="termButtonMenu" style="display:none;position:absolute;top:10px;left:10px;right:55px;bottom:10px;z-index:1000"></div>
                    <div id=p10terminal style="overflow:hidden;position:absolute;top:55px;bottom:0px;width:100%;display:none;background-color:#333">
                        <div id=termTable style="position:absolute;top:0;bottom:0;left:0;right:0">
                            <div id="termarea1">
                                <div class="areaHead" style="line-height:24px">
                                    <div class="toright2">
                                        <input type=button id=termFullScreen value="Full Screen" onclick=deskToggleFull(event) onkeypress="return false" onkeydown="return false" disabled="disabled" style="height:28px;margin-right:3px;">
                                        <div id="terminalCustomUpperRight" style="float:left;margin-right:6px"></div>
                                    </div>
                                    <div>
                                        <span id="connectbutton2span" style="margin-left:3px"><input type="button" id="connectbutton2" cmenu="termConnectButton" value="Connect" style="height:28px" onclick=connectTerminal(event,1) onkeypress="return false" onkeydown="return false" disabled="disabled" /></span>
                                        <span id="disconnectbutton2span" style="margin-left:3px"><input type="button" id="disconnectbutton2" value="Disconnect" style="height:28px" onclick=connectTerminal(event,0) onkeypress="return false" onkeydown="return false" /></span>
                                        <span id="termstatus" style="line-height:22px">Disconnected</span><span id="termtitle"></span>
                                    </div>
                                </div>
                            </div>
                            <div id="termarea3" style="width:100%;height: calc(100% - 60px);" cellpadding=0 cellspacing=0>
                                <div id="termarea3x" style="width:100%;height:100%">
                                    <div style="width:100%;height:100%" id="termarea3xdiv"></div>
                                </div>
                            </div>
                            <div id="termarea4" style="position:relative;height:32px;">
                                <div class="areaFoot">
                                    <div class="toright2"></div>
                                    <div style="height:28px">
                                    </div>
                                </div>
                            </div>
                            <div id=p12TermConsoleMsg style="display:none;cursor:pointer;position:absolute;left:30px;top:45px;color:yellow;background-color:rgba(0,0,0,0.6);padding:10px;border-radius:5px" onclick=p12clearConsoleMsg()></div>
                        </div>
                    </div>
                    <div id=p10files style="position:absolute;top:55px;bottom:0px;width:100%;display:none">
                        <table id="p13toolbar" style="width:100%;height:111px" cellpadding="0" cellspacing="0">
                            <tr>
                                <td style="background-color:#C0C0C0;border-bottom:2px solid black;padding:2px;line-height:24px">
                                    <div style="float:right;text-align:right">
                                        <div id="filesCustomUpperRight" style="float:left;margin-right:6px"></div>
                                    </div>
                                    <div style="margin-left:2px">
                                        <input id=p13AutoConnect value="AutoConnect" onclick=autoConnectFiles(event) onkeypress="return false" onkeydown="return false" type="button" style="display:none">
                                        <input id=p13Connect value="Connect" onclick=connectFiles(event) onkeypress="return false" onkeydown="return false" type="button">
                                        <span class=textOverGray id=p13Status>Disconnected</span>
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td style="width:100%;background-color:#d3d9d6;text-align:left;padding:4px" valign=bottom>
                                    <div style="width:100%;text-align:center">
                                        <input type=button style="width:calc(100%/5 - 5px)" id=p13FolderUp disabled="disabled" onclick="p13folderup()" value="Up" />
                                        <input type=button style="width:calc(100%/5 - 5px)" id=p13SelectAllButton disabled="disabled" onclick="p13selectallfile()" value="SelectAll" onkeypress="return false" onkeydown="return false" />
                                        <input type=button style="width:calc(100%/5 - 5px)" id=p13RenameFileButton disabled="disabled" value="Rename" onclick="p13renamefile()" onkeypress="return false" onkeydown="return false" />
                                        <input type=button style="width:calc(100%/5 - 5px)" id=p13DeleteFileButton disabled="disabled" value="Delete" onclick="p13deletefile()" onkeypress="return false" onkeydown="return false" />
                                        <input type=button style="width:calc(100%/5 - 5px)" id=p13NewFolderButton disabled="disabled" value="Folder" onclick="p13createfolder()" onkeypress="return false" onkeydown="return false" />
                                    </div>
                                    <div style="width:100%;text-align:center">
                                        <input type=button style="width:calc(100%/5 - 5px)" id=p13UploadButton disabled="disabled" value="Upload" onclick="p13uploadFile()" onkeypress="return false" onkeydown="return false" />
                                        <input type=button style="width:calc(100%/5 - 5px)" id=p13CutButton disabled="disabled" value="Cut" onclick="p13copyFile(1)" onkeypress="return false" onkeydown="return false" />
                                        <input type=button style="width:calc(100%/5 - 5px)" id=p13CopyButton disabled="disabled" value="Copy" onclick="p13copyFile(0)" onkeypress="return false" onkeydown="return false" />
                                        <input type=button style="width:calc(100%/5 - 5px)" id=p13PasteButton disabled="disabled" value="Paste" onclick="p13pasteFile()" onkeypress="return false" onkeydown="return false" />
                                        <input type=button style="width:calc(100%/5 - 5px)" id=p13RefreshButton disabled="disabled" value="Refresh" onclick="p13folderup(9999)" onkeypress="return false" onkeydown="return false" />
                                    </div>
                                </td>
                            </tr>
                            <tr>
                                <td style="background-color:#E4E9E7;height:28px">
                                    <table style="width:100%">
                                        <tr>
                                            <td id=p13currentpath style="overflow:hidden;padding-left:4px;padding-top:2px;color:black"></td>
                                            <td style="text-align:right;padding-right:4px">
                                                <select id=p13sortdropdown onchange=p13updateFiles()>
                                                    <option value=1 selected="selected">Sort by name</option>
                                                    <option value=2>Sort by size</option>
                                                    <option value=3>Sort by date</option>
                                                    <option value=4>Descend by name</option>
                                                    <option value=5>Descend by size</option>
                                                    <option value=6>Descend by date</option>
                                                </select>
                                            </td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        </table>
                        <div id=p13FilesConsoleMsg style="display:none;cursor:pointer;position:absolute;left:30px;top:165px;color:yellow;background-color:rgba(0,0,0,0.6);padding:10px;border-radius:5px" onclick=p13clearConsoleMsg()></div>
                        <div id="p13filetable" style="width:100%;height:calc(100% - 133px);overflow:auto;-webkit-user-select:none">
                            <!--
                            <div id="p13bigok" style="width:256px;overflow:hidden;position:absolute;left:337px;top:200px;text-align:center;font-size:1600%;color:#AAAAAA;display:none"><b>&checkmark;</b></div>
                            <div id="p13bigfail" style="width:256px;overflow:hidden;position:absolute;left:337px;top:200px;text-align:center;font-size:1600%;color:#AAAAAA;display:none"><b>&#10007;</b></div>
                            -->
                            <span id="p13files"></span>
                        </div>
                        <table id="p13toolbarBottom" style="width:100%;height:22px;position:absolute;bottom:0px" cellpadding=0 cellspacing=0>
                            <tr><td style="text-align:left;padding:3px;text-align:center;background-color:#D3D9D6;color:black">&nbsp;<span id="p13bottomstatus"></span></td></tr>
                        </table>
                    </div>
                </div>
            </div>
        </div>
        <div id=footer style="height:32px;width:100%;text-align:center;background-color:#113962;position:absolute;bottom:0px">
            <table id=footerMenu cellpadding=0 cellspacing=0 style="height:32px;width:100%;color:white;cursor:pointer;table-layout:fixed"></table>
        </div>
    </div>
    <div id=dialog style="display:none">
        <div style="width:100%;background-color:#003366;color:#FFF;border-radius:5px 5px 0 0">
            <div id=id_dialogclose style=float:right;padding:5px;cursor:pointer onclick=setDialogMode()><b>X</b></div>
            <div id=id_dialogtitle style=padding:5px></div>
            <div style=width:100%;margin:6px></div>
        </div>
        <div style="margin-right:16px;margin-left:8px">
            <div id=dialog1 style="margin:auto;text-align:center;margin:3px">
                <div id=id_dialogMessage style="padding:10px"></div>
            </div>
            <div id=dialog2 style="margin:auto;margin:3px">
                <div id=id_dialogOptions></div>
            </div>
            <div id=dialog3 style="margin:auto;margin:3px">
                <select id="deskkeys" style="width:100%">
                    <option value=10>Ctrl+Alt+Del</option>
                    <option value=11>Tab</option>
                    <option value=5>Win</option>
                    <option value=0>Win+Down</option>
                    <option value=1>Win+Up</option>
                    <option value=2>Win+L</option>
                    <option value=3>Win+M</option>
                    <option value=4>Shift+Win+M</option>
                    <option value=6>Win+R</option>
                    <option value=7>Alt-F4</option>
                    <option value=8>Ctrl-W</option>
                    <option value=9>Alt-Tab</option>
                    <option value=12>Shift-F10</option>
                </select>
            </div>
            <div id=dialog7 style="margin:auto;margin:3px">
                <div id="d7meshkvm">
                    <h4 style="width:100%;border-bottom:1px solid gray">Agent Remote Desktop</h4>
                    <table style="width:100%">
                        <tr>
                            <td>
                                Quality
                            </td>
                            <td style="width:100px">
                                <select id="d7bitmapquality" style="float:right;width:200px" dir="rtl"></select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Scaling
                            </td>
                            <td style="width:100px">
                                <select id="d7bitmapscaling" style="float:right;width:200px" dir="rtl">
                                    <option selected=selected value=1024>100%</option>
                                    <option value=896>87.5%</option>
                                    <option value=768>75%</option>
                                    <option value=640>62.5%</option>
                                    <option value=512>50%</option>
                                    <option value=384>37.5%</option>
                                    <option value=256>25%</option>
                                    <option value=128>12.5%</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td>
                                Rate
                            </td>
                            <td style="width:100px">
                                <select id="d7framelimiter" style="float:right;width:200px" dir="rtl">
                                    <option selected=selected value=50>Fast</option>
                                    <option value=100>Medium</option>
                                    <option value=400>Slow</option>
                                    <option value=1000>Very slow</option>
                                </select>
                            </td>
                        </tr>
                        <tr>
                            <td></td>
                            <td>
                                <label style="display:block" id="d7deskAutoLockLabel"><input type="checkbox" id="d7deskAutoLock" />Lock on Disconnect</label>
                            </td>
                        </tr>
                    </table>
                </div>
                <div id="d7amtkvm">
                    <h4 style="width:100%;border-bottom:1px solid gray">Intel&reg; AMT Hardware KVM</h4>
                    <table style="width:100%">
                        <tr>
                            <td>Encoding</td>
                            <td style="width:100px">
                                <select id="d7desktopmode" style="float:right;width:200px">
                                    <option value="1">RLE8, Fastest</option>
                                    <option value="2">RLE16, Recommended</option>
                                    <option value="3">RAW8, Slow</option>
                                    <option value="4">RAW16, Very Slow</option>
                                </select>
                            </td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
        <div id="idx_dlgButtonBar" style="padding:10px;margin-bottom:20px">
            <input id="idx_dlgCancelButton" type="button" value="Cancel" style="float:right;width:80px;margin-left:5px" onclick="dialogclose(0)">
            <input id="idx_dlgOkButton" type="button" value="OK" style="float:right;width:80px" onclick="dialogclose(1)">
            <div><input id="idx_dlgDeleteButton" type="button" value="Delete" style="display:none" onclick="dialogclose(2)" /></div>
        </div>
    </div>
    <div id=topMenu style="z-index:1000;background-color:#EEE;box-shadow:0px 0px 15px #666;font-family:Arial,Helvetica,sans-serif;border-radius:0px 0px 5px 5px;position:fixed;top:50px;right:5px;width:170px;display:none">
        <div style="padding:12px;border-top:1px solid gray;color:black;cursor:pointer" onclick=topMenu(2)>My Files</div>
        <div style="padding:12px;border-top:1px solid gray;color:black;cursor:pointer" onclick=topMenu(1)>My Account</div>
        <div id="logoutMenuOption"><a href=/logout><div style="padding:12px;border-top:1px solid gray;color:black;cursor:pointer">Logout</div></a></div>
    </div>
    <audio id="chimes"><source src="sounds/chimes.mp3" type="audio/mp3" /></audio>
    <iframe name="fileUploadFrame" style=display:none></iframe>
    <script>
        'use strict';
        var random = '{{{randomlength}}}' // Random length string for BREACH mitigation
        var args = parseUriArgs();
        var urlargs = args;
        var sessionTime = parseInt('{{{sessiontime}}}');
        var sessionRefreshTimer = null;
        var domain = '{{{domain}}}';
        var domainUrl = '{{{domainurl}}}';
        var authCookie = '{{{authCookie}}}';
        var authRelayCookie = '{{{authRelayCookie}}}';
        var viewOnly = parseInt('{{{viewOnly}}}');
        var authCookieRenewTimer = null;
        var serverPublicNamePort = '{{{serverDnsName}}}:{{{serverPublicPort}}}';
        var debugmode = false;
        var attemptWebRTC = false;
        var StatusStrs = ["Disconnected", "Connecting...", "Setup...", "Connected", "Intel&reg; AMT Connected"];
        var files;
        var terminal;
        var sessionActivity = Date.now();
        var deskPinchZoom;
        var deskKeyboardShortcuts = [];
        var xterm = null;
        var xtermfit = null;
        var xtermResizeTimer = null;
        var features = parseInt('{{{features}}}');
        var features2 = parseInt('{{{features2}}}');
        var nodeName = decodeURIComponent('{{{nodeName}}}');

        // Console Message Display Timers
        var p11DeskConsoleMsgTimer = null;
        var p12TermConsoleMsgTimer = null;
        var p13FilesConsoleMsgTimer = null;

        // Check if WebP is supported
        var webpSupport = false;
        check_webp_feature('lossy', function (f, x) { webpSupport = x; });

        function startup() {
            QH('p10deviceName', nodeName);
            if ((features & 32) == 0) {
                // Guard against other site's top frames (web bugs).
                var loc = null;
                try { loc = top.location.toString().toLowerCase(); } catch (e) { }
                if (top != self && (loc == null || top.active == false)) { top.location = self.location; return; }
            }
            if (!args.locale) { var x = getstore('loctag', 0); if ((x != null) && (x != '*')) { args.locale = x; } }

            window.onresize = center;
            center();
            go(10);

            // Set the document title
            if (nodeName.length > 0) { document.title += ' - ' + nodeName; }

            // Document keys
            document.onkeypress = ondeskkeypress;
            document.onkeydown = ondeskkeydown;
            document.onkeyup = ondeskkeyup;
            document.onclick = function (e) { if ((xxdialogMode == 999) && (e.target.id != 'topMenuIcon')) { QV('topMenu', false); xxdialogMode = 0; } }

            // Load desktop settings
            var t = localStorage.getItem('desktopsettings');
            if (t != null) { desktopsettings = JSON.parse(t); }
            applyDesktopSettings();

            attemptWebRTC = false; // For now, default WebRTC off unless we set it in the URL.
            if (args.webrtc != null) { attemptWebRTC = (args.webrtc == 1); }

            // Set the user's desktop shortcut keys
            deskKeyboardShortcuts = [];
            var deskKeyboardShortcutsStr = getstore('deskKeyShortcuts', '0x0A002E,0x100000,0x100028,0x100026,0x10004C,0x10004D,0x11004D,0x100052,0x020073,0x080057,0x020009,0x100025,0x100027').split(',');
            for (var i in deskKeyboardShortcutsStr) { deskKeyboardShortcuts.push(parseInt(deskKeyboardShortcutsStr[i])); }
            updateDeskShortcutKeys();
            updateTermShortcutKeys();
            updateDesktopButtons();
            updateTerminalButtons();

            if (features & 2) { currentDevicePanel = 1; } // Goto desktop
            else if (features & 1) { currentDevicePanel = 5; } // Goto terminal
            else if (features & 4) { currentDevicePanel = 2; } // Goto files
            setupDeviceMenu(currentDevicePanel);
        }

        function setSessionActivity() { sessionActivity = Date.now(); }
        function checkIdleSessionTimeout() {
            var delta = (Date.now() - sessionActivity);
            if (delta > serverinfo.timeout) {
                if (desktop != null) { // Disconnect remote desktop
                    desktop.Stop();
                    desktopNode = desktop = null;
                }
                if (terminal != null) { // Disconnect terminal
                    terminal.Stop();
                    terminal = null;
                }
                if (files != null) { // Disconnect files
                    files.Stop();
                    files = null;
                }
                if (serverinfo.logoutonidlesessiontimeout) {
                    window.location.href = 'logout';
                }
            }
        }

        //
        // Menu System
        //

        function updateFooterMenu(options) {
            while (options != null && options.length < 3) { options.push({ n: '' }); }
            var x = '', prev = '';
            if (options != null) { for (var i in options) { x += '<td style="cursor:pointer' + ((prev == '') ? '' : ';border-left:solid 1px white') + (options[i].s ? ';background-color:#487099' : '') + '" onclick="' + options[i].f + '">' + options[i].n; prev = options[i].n; } }
            QH('footerMenu', '<tr>' + x);
        }


        //
        // MY DEVICES
        //

        var gotKeyPressEvent = false;
        function ondeskkeypress(e, t) {
            setSessionActivity();
            if (desktop && !xxdialogMode && (xxcurrentView == 10) && (currentDevicePanel == 1)) {
                gotKeyPressEvent = true;
                Q('softKeyboard').value = '';
                // Check what keys we are allows to send
                if (viewOnly == 1) return false;
                return desktop.m.handleKeys(e);
            }
            if (terminal && !xxdialogMode && (xxcurrentView == 10) && (currentDevicePanel == 5) && (t !== 1)) {
                if (e.altKey == true) { return true; }
                gotKeyPressEvent = true;
                Q('softKeyboard').value = '';
                var k = 0;
                if (e.charCode != 0) { k = e.charCode; } else if (e.keyCode != 0) { k = e.keyCode; }
                if (k != 0) {
                    if (terminal.urlname == 'sshterminalrelay.ashx') {
                        // SSH
                        terminal.socket.send('~' + String.fromCharCode(k));
                    } else {
                        // Agent
                        terminal.sendText(String.fromCharCode(k));
                    }
                }
                return false;
            }
        }

        function ondeskkeydown(e, t) {
            setSessionActivity();
            if (desktop && !xxdialogMode && (xxcurrentView == 10) && (currentDevicePanel == 1)) {
                gotKeyPressEvent = false;
                Q('softKeyboard').value = '';
                // Check what keys we are allows to send
                if (viewOnly == 1) return false;
                return desktop.m.handleKeyDown(e);
            }
            if (terminal && !xxdialogMode && (xxcurrentView == 10) && (currentDevicePanel == 5) && (t !== 1)) {
                if (e.altKey == true) { return true; }
                Q('softKeyboard').value = '';
                gotKeyPressEvent = false;
                var k = 0;
                if (e.charCode != 0) { k = e.charCode; } else if (e.keyCode != 0) { k = e.keyCode; }
                if (k == 8) { terminal.sendText(String.fromCharCode(k)); } // Enter and backspace
                else if (e.ctrlKey && (k >= 64) && (k <= 95)) {
                    // Ctrl keys
                    if (terminal.urlname == 'sshterminalrelay.ashx') {
                        // SSH
                        terminal.socket.send('~' + String.fromCharCode(k - 64));
                    } else {
                        // Agent
                        terminal.sendText(String.fromCharCode(k - 64));
                    }
                }
            }
        }

        function ondeskkeyup(e, t) {
            setSessionActivity();
            if (desktop && !xxdialogMode && (xxcurrentView == 10) && (currentDevicePanel == 1)) {
                var inputStr = Q('softKeyboard').value;
                Q('softKeyboard').value = '';
                // Check what keys we are allows to send
                if (viewOnly == 1) return;
                if ((gotKeyPressEvent == false) && (inputStr.length > 0) && desktop.m.SendKeyUnicode) {
                    // This is a mobile keyboard, we need to send that is in the input control.
                    var inputchar = inputStr[inputStr.length - 1].charCodeAt(0);
                    desktop.m.SendKeyUnicode(desktop.m.KeyAction.DOWN, inputchar);
                    desktop.m.SendKeyUnicode(desktop.m.KeyAction.UP, inputchar);
                } else {
                    return desktop.m.handleKeyUp(e);
                }
            }
            if (terminal && !xxdialogMode && (xxcurrentView == 10) && (currentDevicePanel == 5) && (gotKeyPressEvent == false) && (t !== 1)) {
                if (e.altKey == true) { return true; }
                var inputStr = Q('softKeyboard').value;
                Q('softKeyboard').value = '';
                if (terminal.urlname == 'sshterminalrelay.ashx') {
                    // SSH
                    terminal.socket.send('~' + inputStr);
                } else {
                    // Agent
                    if (inputStr)
                        terminal.sendText(inputStr);
                }
                return false;
            }
        }


        //
        // MY DEVICE
        //

        var currentDevicePanel = 0;
        function setupDeviceMenu(op, obj) {
            if (op != null) { currentDevicePanel = op; }
            QV('p10desktop', currentDevicePanel == 1); // Show if we have remote control rights or desktop view only rights
            QV('p10files', currentDevicePanel == 2);
            QV('p10terminal', currentDevicePanel == 5);
            var menus = [];
            if (features & 2) { menus.push({ n: "Desktop", f: 'setupDeviceMenu(1)', s: (currentDevicePanel == 1) }); }
            if (features & 1) { menus.push({ n: "Terminal", f: 'setupDeviceMenu(5)', s: (currentDevicePanel == 5) }); }
            if (features & 4) { menus.push({ n: "Files", f: 'setupDeviceMenu(2)', s: (currentDevicePanel == 2) }); }
            updateFooterMenu(menus);
            if (currentDevicePanel == 1) { deskAdjust(); }

            /*
            var meshrights = GetNodeRights(currentNode);
            if (op != null) { currentDevicePanel = op; }
            QV('p10general', currentDevicePanel == 0);
            QV('p10desktop', currentDevicePanel == 1); // Show if we have remote control rights or desktop view only rights
            QV('p10files', currentDevicePanel == 2);
            QV('p10details', currentDevicePanel == 3);
            QV('p10console', currentDevicePanel == 4);
            QV('p10terminal', currentDevicePanel == 5);
            var menus = [];
            if (currentDevicePanel != 0) { menus.push({ n: "General", f: 'setupDeviceMenu(0)' }); }

            if ((currentDevicePanel != 1) &&
                (currentNode != null) &&
                ((meshrights & 8) || (meshrights & 256)) && ((meshrights == 0xFFFFFFFF) || ((meshrights & 65536) == 0)) &&
                (((currentNode.agent == null) && (currentNode.intelamt) && ((typeof currentNode.intelamt.sku !== 'number') || ((currentNode.intelamt.sku & 8) != 0))) || (currentNode.agent && (currentNode.agent.caps & 1)))
            ) { menus.push({ n: "Desktop", f: 'setupDeviceMenu(1)' }); }

            if ((currentDevicePanel != 5) &&
                (currentNode != null) &&
                ((meshrights & 8) || (meshrights & 256)) && ((meshrights == 0xFFFFFFFF) || ((meshrights & 512) == 0)) &&
                (((currentNode.agent == null) && (currentNode.intelamt) && ((typeof currentNode.intelamt.sku !== 'number') || ((currentNode.intelamt.sku & 8) != 0))) || (currentNode.agent && (currentNode.agent.caps & 2)))
            ) { menus.push({ n: "Terminal", f: 'setupDeviceMenu(5)' }); }

            if ((currentDevicePanel != 2) && (currentNode != null) && (meshrights & 8) && ((meshrights == 0xFFFFFFFF) || ((meshrights & 1024) == 0)) && ((currentNode.mtype != 1) && (currentNode.agent) && (currentNode.agent.caps & 4))) { menus.push({ n: "Files", f: 'setupDeviceMenu(2)' }); }
            if ((currentDevicePanel != 3) && (currentNode != null) && (currentNode.mtype < 3) && ((meshrights & 1048576) != 0)) { menus.push({ n: "Details", f: 'setupDeviceMenu(3)' }); }
            if ((currentDevicePanel != 4) && (currentNode != null) && (meshrights & 0x00000010) && (currentNode.mtype == 2)) { menus.push({ n: "Console", f: 'setupDeviceMenu(4)' }); }

            updateFooterMenu(menus);
            if (currentDevicePanel == 1) { deskAdjust(); }
            */
        }


        //
        // DESKTOP
        //

        var desktop;
        var desktopNode;
        var desktopsettings = { encoding: 2, showfocus: false, showmouse: true, showcad: true, quality: 40, scaling: 1024, framerate: 50, autolock: false };
        function setupDesktop() {
            // Setup the remote desktop
            if ((desktopNode != currentNode) && (desktop != null)) { desktop.Stop(); desktopNode = null; desktop = null; }

            // If the device desktop is already connected in multi-desktop, use that.
            if ((desktopNode != currentNode) || (desktop == null)) {
                // Device is not already connected, just setup a blank canvas
                //QH('DeskParent', '<canvas id=Desk width=640 height=200 style="width:100%;-ms-touch-action:none;margin-left:0px" oncontextmenu="return false" onmousedown=dmousedown(event) onmouseup=dmouseup(event) onmousemove=dmousemove(event)></canvas>');
                desktopNode = currentNode;
                // Setup the mouse wheel
                Q('Desk').addEventListener('DOMMouseScroll', function (e) { return dmousewheel(e); });
                Q('Desk').addEventListener('mousewheel', function (e) { return dmousewheel(e); });
            }
            desktopNode = currentNode;
            updateDesktopButtons();

            // On some browsers like IE, we can't save screen shots. Hide the scheenshot/capture buttons.
            if (!Q('Desk')['toBlob']) { QV('deskSaveBtn', false); }
        }

        // Show and enable the right buttons
        function updateDesktopButtons() {
            var deskState = 0;
            if (desktop != null) { deskState = desktop.State; }

            // Show the right buttons
            QV('disconnectbutton1', (deskState != 0));
            QE('deskFullScreen', (deskState != 0));
            QV('connectbutton1', (deskState == 0));
            QV('connectbutton1h', false);
            /*
            QV('connectbutton1h',
                (deskState == 0) &&
                (meshrights & 8) &&
                (
                  ((currentNode.intelamt != null) &&
                  (currentNode.intelamt.state == 2) &&
                  (currentNode.intelamt.ver != null) &&
                  ((currentNode.intelamt.sku == null) ||
                  ((typeof currentNode.intelamt.sku == 'number') &&
                  ((currentNode.intelamt.sku & 8) != 0))))
                )
            );
            */

            // Show the right settings
            QV('d7amtkvm', false);
            QV('d7meshkvm', true);

            // Enable buttons
            QE('connectbutton1', true);
            QE('connectbutton1h', false);
        }

        // Used to translate incoming agent console messages
        var agentConsoleMessages = ['', "Waiting for user to grant access...", "Denied", "Failed to start remote terminal session, {0} ({1})", "Timeout", "Received invalid network data"];
        function formatAgentConsoleMessage(msg, msgid, msgargs) {
            var r;
            if (msgargs == null) { msgargs = []; }
            while (msgargs.length < 3) { msgargs.push(''); } // We need to call the format function in a way that works with older browsers and minifier, can't use apply() or ...
            if (msgid && (msgid < agentConsoleMessages.length)) { r = EscapeHtml(format(agentConsoleMessages[msgid], (msgargs[0]), (msgargs[1]), (msgargs[2]))); } else { r = EscapeHtml(msg); }
            return r.split('\n').join('<br />') + '<br /><br />';
        }

        function connectDesktop(e, contype, tsid, consent) {
            setSessionActivity();
            QV('p11DeskSessionSelector', false);
            p11clearConsoleMsg();
            if (desktop == null) {
                if (contype == 2) {
                    // Setup the Intel AMT remote desktop
                    if ((desktopNode.intelamt.user == null) || (desktopNode.intelamt.user == '')) { editDeviceAmtSettings(desktopNode._id, connectDesktop); return; }
                    desktop = CreateAmtRedirect(CreateAmtRemoteDesktop('Desk'), authCookie);
                    desktop.debugmode = debugmode;
                    desktop.onStateChanged = onDesktopStateChange;
                    desktop.m.stopInput = (viewOnly == 1);
                    desktop.m.bpp = (desktopsettings.encoding == 1 || desktopsettings.encoding == 3) ? 1 : 2;
                    desktop.m.useZRLE = (desktopsettings.encoding < 3);
                    desktop.m.showmouse = true;
                    desktop.m.onScreenSizeChange = function (o, x, y) { if (fullscreen) { QS('deskarea3').width = (x * fullscreenzoom) + 'px'; QS('deskarea3').height = (y * fullscreenzoom) + 'px'; } deskAdjust(); }
                    desktop.Start(desktopNode._id, 16994, '*', '*', 0);
                    desktop.contype = 2;
                } else if ((contype == null) || (contype == 1) || ((contype == 3))) {
                    // Setup the Mesh Agent remote desktop
                    desktop = CreateAgentRedirect(null, CreateAgentRemoteDesktop('Desk'), serverPublicNamePort, authCookie, null, domainUrl);
                    desktop.m.stopInput = (viewOnly == 1);
                    desktop.m.mouseCursorActive(true);
                    desktop.debugmode = debugmode;
                    desktop.m.debugmode = debugmode;
                    desktop.attemptWebRTC = attemptWebRTC;
                    desktop.options = {};
                    if (tsid != null) { desktop.options.tsid = tsid; }
                    if (consent != null) { desktop.options.consent = consent; }
                    if (desktopsettings.autolock == true) { desktop.options.autolock = true; }
                    desktop.onStateChanged = onDesktopStateChange;
                    if ((features2 & 0x2000) != 0) desktop.m.stopInput = true;
                    desktop.onConsoleMessageChange = function () {
                        if (desktop.consoleMessage) {
                            Q('p11DeskConsoleMsg').innerHTML += formatAgentConsoleMessage(desktop.consoleMessage, desktop.consoleMessageId, desktop.consoleMessageArgs);
                            QV('p11DeskConsoleMsg', true);
                            if (p11DeskConsoleMsgTimer != null) { clearTimeout(p11DeskConsoleMsgTimer); }
                            if (desktop.consoleMessageTimeout) { p11DeskConsoleMsgTimer = setTimeout(p11clearConsoleMsg, desktop.consoleMessageTimeout * 1000); }
                        } else {
                            p11clearConsoleMsg();
                        }
                    }
                    desktop.m.ImageType = webpSupport ? 4 : 1; // Send 4 if WebP is supported, otherwise send 1 for JPEG.
                    desktop.m.CompressionLevel = desktopsettings.quality; // Number from 1 to 100. 50 or less is best.
                    desktop.m.ScalingLevel = desktopsettings.scaling;
                    desktop.m.FrameRateTimer = desktopsettings.framerate;
                    desktop.m.onDisplayinfo = deskDisplayInfo;
                    desktop.m.onScreenSizeChange = function (o, x, y) { if (fullscreen) { QS('deskarea3').width = (x * fullscreenzoom) + 'px'; QS('deskarea3').height = (y * fullscreenzoom) + 'px'; } deskAdjust(); }
                    desktop.Start(null);
                    desktop.contype = 1;
                } else if (contype == 3) {
                    // Ask for user sessions
                    //meshserver.send({ action: 'msg', type: 'userSessions', nodeid: currentNode._id, tag: consent });
                }
            } else {
                // Disconnect and clean up the remote desktop
                desktop.Stop();
                desktopNode = desktop = null;
            }
        }

        function p11clearConsoleMsg() { QH('p11DeskConsoleMsg', ''); QV('p11DeskConsoleMsg', false); if (p11DeskConsoleMsgTimer) { clearTimeout(p11DeskConsoleMsgTimer); p11DeskConsoleMsgTimer = null; } }
        function p12clearConsoleMsg() { QH('p12TermConsoleMsg', ''); QV('p12TermConsoleMsg', false); if (p12TermConsoleMsgTimer) { clearTimeout(p12TermConsoleMsgTimer); p12TermConsoleMsgTimer = null; } }
        function p13clearConsoleMsg() { QH('p13FilesConsoleMsg', ''); QV('p13FilesConsoleMsg', false); if (p13FilesConsoleMsgTimer) { clearTimeout(p13FilesConsoleMsgTimer); p13FilesConsoleMsgTimer = null; } }

        function p12setConsoleMsg(msg, timeout) {
            if (msg) {
                Q('p12TermConsoleMsg').innerHTML += msg;
                QV('p12TermConsoleMsg', true);
                if (p12TermConsoleMsgTimer != null) { clearTimeout(p12TermConsoleMsgTimer); }
                if (timeout) { p12TermConsoleMsgTimer = setTimeout(p12clearConsoleMsg, timeout); }
            } else {
                p12clearConsoleMsg();
            }
        }

        function p13setConsoleMsg(msg, timeout) {
            if (msg) {
                Q('p13FilesConsoleMsg').innerHTML += msg;
                QV('p13FilesConsoleMsg', true);
                if (p13FilesConsoleMsgTimer != null) { clearTimeout(p13FilesConsoleMsgTimer); }
                if (timeout) { p13FilesConsoleMsgTimer = setTimeout(p13clearConsoleMsg, timeout); }
            } else {
                p13clearConsoleMsg();
            }
        }

        function onDesktopStateChange(xdesktop, state) {
            var xstate = state;
            if ((xstate == 3) && (xdesktop.contype == 2)) { xstate++; }
            var str = StatusStrs[xstate];
            if ((desktop != null) && (desktop.webRtcActive == true)) { str += ", WebRTC"; }
            //if (desktop.m.stopInput == true) { str += ', Loopback'; }
            QH('deskstatus', str);
            switch (state) {
                case 0:
                    // Disconnect and clean up the remote desktop
                    desktop.Stop();
                    desktopNode = desktop = null;
                    QV('DeskScreens', false);
                    if (fullscreen == true) { deskToggleFull(); }
                    break;
                case 2:
                    break;
                default:
                    //console.log('Unknown onDesktopStateChange state', state);
                    break;
            }
            updateDesktopButtons();
            deskAdjust();
            setTimeout(deskAdjust, 50);
        }

        function showDesktopSettings() {
            if (xxdialogMode) return;
            applyDesktopSettings();
            updateDesktopButtons();
            setDialogMode(7, "Remote Desktop Settings", 3, showDesktopSettingsChanged);
        }

        function showDesktopSettingsChanged() {
            desktopsettings.encoding = d7desktopmode.value;
            desktopsettings.quality = d7bitmapquality.value;
            desktopsettings.scaling = d7bitmapscaling.value;
            desktopsettings.framerate = d7framelimiter.value;
            desktopsettings.autolock = d7deskAutoLock.checked;
            localStorage.setItem('desktopsettings', JSON.stringify(desktopsettings));
            applyDesktopSettings();
            if (desktop) {
                if (desktop.contype == 1) {
                    if (desktop.State != 0) { desktop.m.SendCompressionLevel(webpSupport ? 4 : 1, desktopsettings.quality, desktopsettings.scaling, desktopsettings.framerate); }
                    desktop.sendCtrlMsg('{"ctrlChannel":"102938","type":"autolock","value":' + desktopsettings.autolock + '}');
                }
                if (desktop.contype == 2) {
                    if (desktop.State != 0) { desktop.Stop(); setTimeout(function () { connectDesktop(null, 2); }, 50); }
                }
            }
        }

        function applyDesktopSettings() {
            var r = '', ops = (features & 512) ? [100, 90, 70, 50, 40, 30, 20, 10, 5, 1] : [50, 40, 30, 20, 10, 5, 1];
            for (var i in ops) { r += '<option value=' + ops[i] + '>' + ops[i] + '%</option>'; }
            QH('d7bitmapquality', r);
            d7desktopmode.value = desktopsettings.encoding;
            d7bitmapquality.value = 40; // Default value
            if (ops.indexOf(parseInt(desktopsettings.quality)) >= 0) { d7bitmapquality.value = desktopsettings.quality; }
            d7bitmapscaling.value = desktopsettings.scaling;
            if (desktopsettings.framerate) { d7framelimiter.value = desktopsettings.framerate; }
            if (desktopsettings.autolock != null) { d7deskAutoLock.checked = desktopsettings.autolock; }
        }


        var keyboardShown = false;
        var keyboardShownTimer = null;
        var fullScreenMode = false;
        function toggleKeyboard() {
            if (xxdialogMode) return;
            if (keyboardShownTimer != null) { clearTimeout(keyboardShownTimer); }
            if (keyboardShown) { Q('softKeyboard').blur(); keyboardShown = false; } else { Q('softKeyboard').focus(); keyboardShown = true; }
            QV('deskkeybutton2a', fullscreen && !keyboardShown);
            QV('deskkeybutton2b', fullscreen && keyboardShown);
        }

        function keyboardFocusChange() {
            keyboardShownTimer = setTimeout(function () {
                keyboardShownTimer = null;
                keyboardShown = (Q('softKeyboard') == document.activeElement);
                QV('deskkeybutton2a', fullscreen && !keyboardShown);
                QV('deskkeybutton2b', fullscreen && keyboardShown);
            }, 10);
        }

        function exitButton() {
            if (xxdialogMode) return;
            QV('deskButtonMenu', false);
            QV('termButtonMenu', false);
            deskToggleFull();
        }

        function deskMenuButton(x) {
            toggleMenu(true);
            deskSendKeys(x);
        }

        //
        // Desktop Shortcut Keys
        //

        function updateDeskShortcutKeys() {
            var x = '<div class="menuButton" onclick="deskMenuButton(-1)">' + "Customize" + '</div>';
            for (var i in deskKeyboardShortcuts) { x += '<div class="menuButton" onclick="deskMenuButton(' + deskKeyboardShortcuts[i] + ')">' + keyShortcutTotext(deskKeyboardShortcuts[i]) + '</div>'; }
            QH('deskButtonMenu', x);
        }

        var keyStrings = { 8: "BackSpace", 9: "Tab", 13: "Enter", 27: "Escape", 44 : "Print Screen", 45: "Insert", 46: "Del", 36: "Home", 35: "End", 33: "Page Up", 34: "Page Down", 37: "Left", 38: "Up", 39: "Right", 40: "Down", 0: "None" }

        function keyShortcutTotext(n) {
            var x = [];
            if (n & 0x010000) { x.push("Shift"); }
            if (n & 0x020000) { x.push("Alt"); }
            if (n & 0x080000) { x.push("Ctrl"); }
            if (n & 0x100000) { x.push("Win"); }
            n = (n & 0xFFFF);
            if ((n >= 112) && (n <= 123)) { x.push('F' + (n - 111)); } // Fx keys
            else if ((n != 0) && (keyStrings[n])) { x.push(keyStrings[n]); }
            else { if (n != 0) { x.push(String.fromCharCode(n)); } }
            return x.join(' + ');
        }

        // Customize keyboard shortcuts
        function deskCustomizeKeys() {
            if (xxdialogMode) return;
            var x = '<div id=d2shortcuts style="width:100%;height:180px;padding:4px;overflow-y:auto;border:1px solid gray"></div><div style=width:100%;padding:5px>';
            x += '<label><input id=d1kshift type=checkbox /> ' + "Shift" + '</label><label> <input id=d1kalt type=checkbox /> ' + "Alt" + '</label><label> <input id=d1kctrl type=checkbox /> ' + "Ctrl" + '</label> <input id=d1kwin type=checkbox /> ' + "Win" + '</label>';
            x += ' <select id=d2keySelect>';
            for (var i in keyStrings) { x += '<option value=' + i + '>' + keyStrings[i] + '</option>'; }
            for (var i = 1; i <= 12; i++) { x += '<option value=' + (i + 111) + '>F' + i + '</option>'; }
            for (var i = 0; i < 10; i++) { x += '<option value=' + (i + 48) + '>' + i + '</option>'; }
            for (var i = 0; i < 26; i++) { x += '<option value=' + (i + 65) + '>' + String.fromCharCode(i + 65) + '</option>'; }
            x += '</select> <input type=button value=' + "Add" + ' onclick=addDeskCustomizeKey() /></div>';
            QH('p10dialog2', x);
            xxdialogMode = 2;
            QV('p10dialog', true);
            deskUpdateShortcutList();
        }

        function deskCustomizeKeysEx() {
            QV('p10dialog', false);
            xxdialogMode = 0;
            putstore('deskKeyShortcuts', deskKeyboardShortcuts.join(','));
            updateDeskShortcutKeys();
        }

        function deskUpdateShortcutList() {
            var x = '';
            for (var i in deskKeyboardShortcuts) {
                var kt = keyShortcutTotext(deskKeyboardShortcuts[i]), orderButtons = '';
                if (i != (deskKeyboardShortcuts.length - 1)) { orderButtons += '<img width=8 height=8 style=float:right;cursor:pointer;padding:3px src="images/c2.png" onclick=deskCustomizeKeyDown(' + deskKeyboardShortcuts[i] + ')>'; }
                if (i != 0) { orderButtons += '<img width=8 height=8 style=float:right;cursor:pointer;padding:3px src="images/c3.png" onclick=deskCustomizeKeyUp(' + deskKeyboardShortcuts[i] + ')>'; }
                x += '<div style="width:100%;background-color:#AAA;border-radius:4px;margin-bottom:4px;padding:4px;text-align:left;box-sizing:border-box" value=' + deskKeyboardShortcuts[i] + '>' + kt + '<img width=10 height=10 style=float:right;cursor:pointer;padding:2px;margin-left:8px src="images/trash.png" onclick=removeDeskCustomizeKey(' + deskKeyboardShortcuts[i] + ')>' + orderButtons + '</div>';
            }
            if (x == '') { x = '<i>' + "No keyboard shortcuts defined" + '</i>'; }
            QH('d2shortcuts', x);
        }

        function deskCustomizeKeyDown(k) {
            var i = deskKeyboardShortcuts.indexOf(k), x = deskKeyboardShortcuts[i + 1];
            deskKeyboardShortcuts[i + 1] = deskKeyboardShortcuts[i];
            deskKeyboardShortcuts[i] = x;
            deskUpdateShortcutList();
        }

        function deskCustomizeKeyUp(k) {
            var i = deskKeyboardShortcuts.indexOf(k), x = deskKeyboardShortcuts[i];
            deskKeyboardShortcuts[i] = deskKeyboardShortcuts[i - 1];
            deskKeyboardShortcuts[i - 1] = x;
            deskUpdateShortcutList();
        }

        function removeDeskCustomizeKey(k) {
            var na = [];
            for (var i in deskKeyboardShortcuts) { if (deskKeyboardShortcuts[i] != k) { na.push(deskKeyboardShortcuts[i]); } }
            deskKeyboardShortcuts = na;
            deskUpdateShortcutList();
        }

        function addDeskCustomizeKey() {
            var k = parseInt(Q('d2keySelect').value);
            if (Q('d1kshift').checked) { k |= 0x010000; }
            if (Q('d1kalt').checked) { k |= 0x020000; }
            if (Q('d1kctrl').checked) { k |= 0x080000; }
            if (Q('d1kwin').checked) { k |= 0x100000; }
            if ((k > 0) && (deskKeyboardShortcuts.indexOf(k) == -1)) { deskKeyboardShortcuts.push(k); deskUpdateShortcutList(); }
        }

        // Remote desktop special key combos for Windows
        function deskSendKeys(ks) {
            if (xxdialogMode || desktop == null || desktop.State != 3) return;

            // Construct the key command
            if (ks == -1) { deskCustomizeKeys(); return; } // Customize
            if (ks == 0x0A002E) { desktop.m.sendcad(); return; } // CTRL-ALT-DEL
            //if ((desktop.contype == 1) && (ks == 0x10004C)) { desktop.sendCtrlMsg('{"action":"lock"}'); return; } // Lock desktop, WIN + L

            var flags = (ks & 0xFF0000) >> 16, key = (ks & 0xFFFF), keyArray = [], keyArray2 = [];
            var amtTranslate = {
                8: 0xff08, // BackSpace
                9: 0xff09, // Tab
                13: 0xff0d, // Return or Enter
                27: 0xff1b, // Escape
                45: 0xff63, // Insert
                46: 0xffff, // Delete
                36: 0xff50, // Home
                35: 0xff57, // End
                33: 0xff55, // Page Up
                34: 0xff56, // Page Down
                37: 0xff51, // Left arrow
                38: 0xff52, // Up arrow
                39: 0xff53, // Right arrow
                40: 0xff54, // Down arrow
                112: 0xffbe, // F1
                113: 0xffbf, // F2
                114: 0xffc0, // F3
                115: 0xffc1, // F4
                116: 0xffc2, // F5
                117: 0xffc3, // F6
                118: 0xffc4, // F7
                119: 0xffc5, // F8
                120: 0xffc6, // F9
                121: 0xffc7, // F10
                122: 0xffc8, // F11
                123: 0xffc9  // F12
            }

            // 0x010000 = Shift
            // 0x020000 = Left-Alt
            // 0x080000 = Ctrl
            // 0x100000 = Window

            if (desktop.contype == 2) {
                // Intel AMT
                if (flags & 1) { keyArray.push([0xffe1, 1]); keyArray2.push([0xffe1, 0]); } // Shift
                if (flags & 2) { keyArray.push([0xffe9, 1]); keyArray2.push([0xffe9, 0]); } // Left-alt
                if (flags & 8) { keyArray.push([0xffe3, 1]); keyArray2.push([0xffe3, 0]); } // Ctrl
                if (flags & 16) { keyArray.push([0xffe7, 1]); keyArray2.push([0xffe7, 0]); } // Windows key
                if (amtTranslate[key]) { key = amtTranslate[key]; }
                if ((key >= 65) && (key <= 90)) { key += 32; }
                if (key != 0) { keyArray.push([key, 1]); keyArray2.push([key, 0]); }
                keyArray2.reverse();
                for (var i = 0; i < keyArray2.length; i++) { keyArray.push(keyArray2[i]); }
                desktop.m.sendkey(keyArray);
            } else {
                // Agent desktop
                if (flags & 1) { keyArray.push([desktop.m.KeyAction.DOWN, 16]); keyArray2.push([desktop.m.KeyAction.UP, 16]); } // Shift
                if (flags & 2) { keyArray.push([desktop.m.KeyAction.EXDOWN, 18]); keyArray2.push([desktop.m.KeyAction.EXUP, 18]); } // Left-alt
                if (flags & 8) { keyArray.push([desktop.m.KeyAction.EXDOWN, 17]); keyArray2.push([desktop.m.KeyAction.EXUP, 17]); } // Ctrl
                if (flags & 16) { keyArray.push([desktop.m.KeyAction.EXDOWN, 0x5B]); keyArray2.push([desktop.m.KeyAction.EXUP, 0x5B]); } // Windows key
                if (key != 0) { keyArray.push([desktop.m.KeyAction.DOWN, key]); keyArray2.push([desktop.m.KeyAction.UP, key]); }
                keyArray2.reverse();
                for (var i = 0; i < keyArray2.length; i++) { keyArray.push(keyArray2[i]); }
                desktop.m.SendKeyMsgKC(keyArray);
            }
        }

        function toggleMenu(x) {
            if (xxdialogMode) return;
            QV('deskButtonMenu', fullscreen && !x && (currentDevicePanel == 1));
            QV('termButtonMenu', fullscreen && !x && (currentDevicePanel == 5));
            QV('deskkeybutton3a', fullscreen && x);
            QV('deskkeybutton3b', fullscreen && !x);
        }

        function deskChangeMouseButton(x) {
            if (xxdialogMode) return;
            if (desktop == null) return;
            desktop.m.SwapMouse = !desktop.m.SwapMouse;
            QV('deskkeybutton4a', fullscreen && (!desktop.m.SwapMouse));
            QV('deskkeybutton4b', fullscreen && (desktop.m.SwapMouse));
        }

        function deskChangeFullscreenZoom() {
            if (xxdialogMode) return;
            if (currentDevicePanel == 1) {
                if (desktop == null) return;
                if (fullscreenzoom == 1) { fullscreenzoom = 0.5; } else { fullscreenzoom = 1; }
                QV('deskkeybutton5a', fullscreen && (fullscreenzoom == 1));
                QV('deskkeybutton5b', fullscreen && (fullscreenzoom != 1));
                QS('deskarea3').width = (desktop.m.ScreenWidth * fullscreenzoom) + 'px';
                QS('deskarea3').height = (desktop.m.ScreenHeight * fullscreenzoom) + 'px';
                deskAdjust();
            }
            if (currentDevicePanel == 5) {
                if (terminal == null) return;
                xterm.setOption('fontSize', (xterm.getOption('fontSize') == 15) ? 10 : 15)
            }
        }

        var fullscreen = false;
        var fullscreenzoom = 1;
        function deskToggleFull() {
            fullscreen = !fullscreen;
            QV('mastheadx', !fullscreen);
            QV('masthead', !fullscreen);
            QV('topbar', !fullscreen);
            QV('p11deviceNameHeader', !fullscreen);
            QV('footer', !fullscreen);
            QV('column_l_bottomgap', !fullscreen);
            QV('idx_deskFullBtn2', fullscreen);
            QV('deskFullBtn', !fullscreen);
            QV('p10deskTopTable', !fullscreen);
            QV('deskarea1', !fullscreen);
            QV('deskarea4', !fullscreen);
            QV('termarea1', !fullscreen);
            QV('termarea4', !fullscreen);
            var inputAllowed = (viewOnly == 0);

            // Show full screen buttons if needed
            QV('deskkeybutton1', fullscreen);
            if (currentDevicePanel == 1) { // Desktop panel is being shown (1 = Desktop, 5 = Terminal)
                // Move shortcut key to desktop position
                QS('deskkeybutton2a').top = QS('deskkeybutton2b').top = '210px';
                // Move the zoom button to normal or top position
                QS('deskkeybutton5a').top = QS('deskkeybutton5b').top = (inputAllowed) ? '160px' : '60px'; // Zoom
                QV('deskkeybutton2a', fullscreen && inputAllowed);
                QV('deskkeybutton2b', false);
                QV('deskkeybutton3a', fullscreen && inputAllowed);
                QV('deskkeybutton3b', false);
                QV('deskkeybutton4a', fullscreen && inputAllowed && (!desktop.m.SwapMouse));
                QV('deskkeybutton4b', fullscreen && inputAllowed && (desktop.m.SwapMouse));
                QV('deskkeybutton5a', fullscreen && (fullscreenzoom == 1));
                QV('deskkeybutton5b', fullscreen && (fullscreenzoom != 1));
            }
            if (currentDevicePanel == 5) {
                // Move right buttons to terminal position
                //QS('deskkeybutton3a').top = QS('deskkeybutton3b').top = '60px'; // Shortcuts
                //QS('deskkeybutton5a').top = QS('deskkeybutton5b').top = '110px'; // Zoom
                QS('deskkeybutton2a').top = QS('deskkeybutton2b').top = '110px'; // Keyboard
                QV('deskkeybutton2a', fullscreen);
                QV('deskkeybutton2b', false);
                QV('deskkeybutton3a', fullscreen);
                QV('deskkeybutton3b', false);
                QV('deskkeybutton4a', false);
                QV('deskkeybutton4b', false);
                QV('deskkeybutton5a', false);
                QV('deskkeybutton5a', false);
                //QV('deskkeybutton5a', xterm.getOption('fontSize') == 15);
                //QV('deskkeybutton5b', xterm.getOption('fontSize') != 15);
            }

            if (fullscreen) {
                QS('DeskParent').height = null;
                QS('page_content').top = '0px';
                QS('page_content').bottom = '0px';
                if (currentDevicePanel == 1) {
                    QS('p10desktop').top = '0px';
                    QS('p10desktop').overflow = 'scroll';
                    QS('deskarea3').top = '0px';
                    QS('deskarea3').width = (desktop.m.ScreenWidth * fullscreenzoom) + 'px';
                    QS('deskarea3').height = (desktop.m.ScreenHeight * fullscreenzoom) + 'px';
                    QS('deskarea3')['padding-right'] = '55px';
                }
                if (currentDevicePanel == 5) {
                    QS('p10terminal').top = '0px';
                    QS('p10terminal').overflow = 'scroll';
                    QS('termarea3').top = '0px';
                    QS('termarea3').bottom = null;
                    QS('termarea3').right = null;
                    QS('termarea3')['padding-right'] = '55px';
                    QS('termarea3')['height'] = '100%';
                }
                QS('body')['background-color'] = '#000';
                QS('p10')['background-color'] = '#000';
            } else {
                QS('DeskParent').height = '100%';
                QS('page_content').top = '50px';
                QS('page_content').bottom = '32px';
                if (currentDevicePanel == 1) {
                    QS('p10desktop').top = '55px';
                    QS('p10desktop').overflow = 'hidden';
                    QS('deskarea3').top = '32px';
                    QS('deskarea3').left = null;
                    QS('deskarea3').width = '100%';
                    QS('deskarea3').height = 'calc(100% - 64px)';
                    QS('deskarea3')['padding-right'] = '';
                    QS('DeskParent')['margin-top'] = null;
                    QS('DeskParent')['margin-left'] = null;
                }
                if (currentDevicePanel == 5) {
                    //xterm.setOption('fontSize', 15)
                    QS('p10terminal').top = '55px';
                    QS('p10terminal').overflow = 'hidden';
                    Q('p10terminal').scrollTop = 0;
                    Q('p10terminal').scrollLeft = 0;
                    QS('termarea3').top = '32px';
                    QS('termarea3').bottom = '32px';
                    //QS('termarea3').right = '0px';
                    QS('termarea3')['padding-right'] = null;
                    QS('termarea3')['height'] = 'calc(100% - 60px)';
                }
                QS('body')['background-color'] = '#FFF';
                QS('p10')['background-color'] = null;
            }
            if (currentDevicePanel == 1) { deskAdjust(); }
        }

        function deskAdjust() {
            if (currentDevicePanel != 1) return; // If not on desktop tab, ignore this.
            if (fullscreen) {
                QS('Desk')['margin-top'] = null;
                QS('Desk')['margin-bottom'] = null;
                QS('Desk').width = '100%';
                QS('Desk').height = '100%';
                var parentH = Q('p10desktop').clientHeight, parentW = Q('p10desktop').clientWidth;
                var deskH = Q('deskarea3').clientHeight, deskW = Q('deskarea3').clientWidth - 55;
                if (parentH > deskH) { QS('deskarea3').top = ((parentH - deskH) / 2) + 'px'; } else { QS('deskarea3').top = null; }
                if (parentW > deskW) { QS('deskarea3').left = ((parentW - deskW) / 2) + 'px'; } else { QS('deskarea3').left = null; }
            } else {
                var parentH = Q('DeskParent').clientHeight, parentW = Q('DeskParent').clientWidth;
                var deskH = Q('Desk').height, deskW = Q('Desk').width;
                var webPageFullScreen = false;

                // Fixed aspect ratio
                if ((parentH / parentW) > (deskH / deskW)) {
                    var hNew = ((deskH * parentW) / deskW) + 'px';
                    QS('Desk').height = hNew;
                    QS('Desk').width = '100%';
                } else {
                    var wNew = ((deskW * parentH) / deskH) + 'px';
                    QS('Desk').width = wNew;
                    QS('Desk').height = '100%';
                }
                QS('DeskParent').overflow = 'hidden';

                // Adjust top/bottom margins
                var x = (Q('DeskParent').clientHeight - Q('Desk').clientHeight) / 2;
                QS('Desk')['margin-top'] = x + 'px';
                QS('Desk')['margin-bottom'] = x + 'px';
            }
        }

        function sendSpecialKeys() {
            if (xxdialogMode || desktop == null || desktop.State != 3) return;
            setDialogMode(3, "Special Keys", 3, deskSendKeys);
        }

        // Save the desktop image to file
        function deskSaveImage() {
            setSessionActivity();
            if (xxdialogMode || desktop == null || desktop.State != 3) return;
            var d = new Date(), n = 'Desktop-' + currentNode.name + '-' + d.getFullYear() + '-' + ('0' + (d.getMonth() + 1)).slice(-2) + '-' + ('0' + d.getDate()).slice(-2) + '-' + ('0' + d.getHours()).slice(-2) + '-' + ('0' + d.getMinutes()).slice(-2);
            Q('Desk')['toBlob'](function (blob) { saveAs(blob, n + '.png'); });
        }

        function deskSelectScreens() {
            if (xxdialogMode || desktop == null || desktop.State != 3) return;
            var x = '', info = desktop.m.displays;
            for (var i in info) { x += '<option value=' + i + ' ' + ((desktop.m.selectedDisplay == i) ? ' selected' : '') + '>' + info[i] + '</option>'; }
            x = addHtmlValue4("Screen", '<select style=width:100% id=deskdisplays>' + x + '</select>');
            setDialogMode(2, "Screen Selection", 3, deskSelectScreensEx, x);
        }

        function deskSelectScreensEx() {
            if (desktop == null || desktop.State != 3) return;
            desktop.m.SetDisplay(parseInt(Q('deskdisplays').value));
        }

        function deskDisplayInfo(sender, info, selDisplay, selItem) {
            var displayCount = 0;
            for (var x in info) { displayCount++; }
            QV('DeskScreens', displayCount > 1);
        }

        function dmousedown(e) { setSessionActivity(); if ((!xxdialogMode && desktop != null)) { if (fullscreen) { e.addx = Q('p10desktop').scrollLeft * (1 / fullscreenzoom); e.addy = Q('p10desktop').scrollTop * (1 / fullscreenzoom); } desktop.m.mousedown(e); } }
        function dmouseup(e) { setSessionActivity(); if ((!xxdialogMode && desktop != null)) { if (fullscreen) { e.addx = Q('p10desktop').scrollLeft * (1 / fullscreenzoom); e.addy = Q('p10desktop').scrollTop * (1 / fullscreenzoom); } desktop.m.mouseup(e); } }
        function dmousemove(e) { setSessionActivity(); if ((!xxdialogMode && desktop != null)) { if (fullscreen) { e.addx = Q('p10desktop').scrollLeft * (1 / fullscreenzoom); e.addy = Q('p10desktop').scrollTop * (1 / fullscreenzoom); } desktop.m.mousemove(e); } }
        function dmousewheel(e) { setSessionActivity(); if ((!xxdialogMode && desktop != null) && desktop.m.mousewheel) { if (fullscreen) { e.addx = Q('p10desktop').scrollLeft * (1 / fullscreenzoom); e.addy = Q('p10desktop').scrollTop * (1 / fullscreenzoom); } desktop.m.mousewheel(e); haltEvent(e); return true; } return false; }
        function drotate(x) { if (!xxdialogMode && desktop != null) { desktop.m.setRotation(desktop.m.rotation + x); deskAdjust(); } }


        //
        // TERMINAL
        //

        var terminalNode;
        function setupTerminal() {
            // Setup the terminal
            if ((terminalNode != currentNode) && (terminal != null)) { terminal.Stop(); terminal = null; }
            terminalNode = currentNode;
            updateTerminalButtons();
        }

        // Show and enable the right buttons
        function updateTerminalButtons() {
            var termState = ((terminal != null) && (terminal.state != 0));
            QE('termFullScreen', (termState != 0));

            // Show the right buttons
            QV('disconnectbutton2span', termState == true);
            QV('connectbutton2span', termState == false);

            // Enable buttons
            QE('connectbutton2', true);
        }

        // Called when the terminal state changes
        function onTerminalStateChange(xterminal, state) {
            var xstate = state;
            if ((xstate == 3) && (xterminal.contype == 2)) { xstate++; }
            var str = StatusStrs[xstate];
            if (terminal.webRtcActive == true) { str += ", WebRTC"; }
            QH('termstatus', str);
            switch (state) {
                case 0:
                    // Disconnected, clear the terminal
                    xterm.dispose();
                    xterm = null;
                    if (terminal != null) { terminal.Stop(); terminal = null; }
                    break;
                case 3:
                    xterm.focus();
                    break;
                default:
                    //console.log('Unhandled onTerminalStateChange state', state);
                    break;
            }
            updateTerminalButtons();
        }

        // Handles a tunnel to a remote shell
        function CreateRemoteTunnel(onTunnelUpdate, options) {
            var obj = { protocol: 1 };
            if ((options != null) && (typeof options.protocol == 'number')) { obj.protocol = options.protocol; }
            obj.onTunnelUpdate = onTunnelUpdate;
            obj.xxStateChange = function (state) { }
            obj.ProcessBinaryData = function (data) { obj.onTunnelUpdate(data); }
            obj.ProcessData = function (data) { obj.onTunnelUpdate(data); }
            obj.terminalEmulation = 1;
            obj.fxEmulation = 0;
            obj.lineFeed = '\r\n';
            return obj;
        }

        function tunnelUpdate(data) {
            if (xterm.writeUtf8) {
                if (typeof data == 'string') { xterm.writeUtf8(data); } else { xterm.writeUtf8(new Uint8Array(data)); }
            } else {
                if (typeof data == 'string') { xterm.write(data); } else { xterm.write(new Uint8Array(data)); }
            }
        }

        function sshTunnelUpdate(data) {
            if (typeof data == 'string') {
                if (data[0] == '{') {
                    var j = JSON.parse(data);
                    switch (j.action) {
                        case 'sshauth': {
                            var x = '';
                            x += addHtmlValue("Authentication", '<select id=dp2authmethod style=width:150px onchange=sshAuthUpdate(event)><option value=1 selected>' + "Username & Password" + '</option><option value=2>' + "Username and Key" + '</option></select>')
                            x += addHtmlValue("Username", '<input id=dp2user style=width:150px maxlength=64 autocomplete=off onkeyup=sshAuthUpdate(event) />');
                            x += '<div id=d2passauth>';
                            x += addHtmlValue("Password", '<input type=password id=dp2pass style=width:150px maxlength=64 autocomplete=off onkeyup=sshAuthUpdate(event) />');
                            x += '</div><div id=d2keyauth style=display:none>';
                            x += addHtmlValue("Key File", '<input type=file id=dp2key style=width:150px maxlength=64 autocomplete=off onchange=sshAuthUpdate(event) />');
                            x += addHtmlValue("Key Password", '<input type=password id=dp2keypass style=width:150px maxlength=64 autocomplete=off onkeyup=sshAuthUpdate(event) />');
                            x += '</div>';
                            x += '<label><input id=dp2keep type=checkbox>' + "Remember credentials" + '</label>';
                            x += '<div id=d2keyauth2 style=font-size:x-small><br />' + "Key file must be in OpenSSH format." + '</div>';
                            setDialogMode(2, "Authentication", 11, sshConnectEx, x, 'ssh');
                            setTimeout(sshAuthUpdate, 50);
                            break;
                        }
                        case 'sshautoauth': {
                            terminal.socket.send(JSON.stringify({ action: 'sshautoauth', cols: xterm.cols, rows: xterm.rows, width: Q('termarea3xdiv').offsetWidth, height: Q('termarea3xdiv').offsetHeight }));
                            break;
                        }
                        case 'autherror': { p12setConsoleMsg("Authentication Error", 5000); break; }
                        case 'sessionerror': { p12setConsoleMsg("Session expired", 5000); break; }
                        case 'sessiontimeout': { p12setConsoleMsg("Session timeout", 5000); break; }
                    }
                } else if (data[0] == '~') { xterm.writeUtf8(data.substring(1)); }
            }
        }

        function sshAuthUpdate(e) {
            QV('d2passauth', Q('dp2authmethod').value == 1);
            QV('d2keyauth', Q('dp2authmethod').value == 2);
            QV('d2keyauth2', Q('dp2authmethod').value == 2);
            if (Q('dp2authmethod').value == 1) {
                QE('idx_dlgOkButton', (Q('dp2user').value.length > 0) && (Q('dp2pass').value.length > 0));
            } else {
                QE('idx_dlgOkButton', false);
                var ok = (Q('dp2user').value.length > 0) && (Q('dp2key').files != null) && (Q('dp2key').files.length == 1) && (Q('dp2key').files[0].size < 8000);
                if (ok == true) {
                    var reader = new FileReader();
                    reader.onload = function (e) {
                        var validkey = ((e.target.result.indexOf('-----BEGIN OPENSSH PRIVATE KEY-----') >= 0) && (e.target.result.indexOf('-----END OPENSSH PRIVATE KEY-----') >= 0));
                        QE('idx_dlgOkButton', validkey);
                    }
                    reader.readAsText(Q('dp2key').files[0]);
                }
            }
        }
        function sshConnectEx(b) {
            if (b == 0) {
                if (terminal != null) { connectTerminal(); } // Disconnect
            } else {
                if (Q('dp2authmethod').value == 1) {
                    terminal.socket.send(JSON.stringify({ action: 'sshauth', username: Q('dp2user').value, password: Q('dp2pass').value, keep: Q('dp2keep').checked, cols: xterm.cols, rows: xterm.rows, width: Q('termarea3xdiv').offsetWidth, height: Q('termarea3xdiv').offsetHeight }));
                } else {
                    var reader = new FileReader(), username = Q('dp2user').value, keypass = Q('dp2keypass').value, keep = Q('dp2keep').checked;
                    reader.onload = function (e) { terminal.socket.send(JSON.stringify({ action: 'sshauth', username: username, keypass: keypass, key: e.target.result, keep: keep, cols: xterm.cols, rows: xterm.rows, width: Q('termarea3xdiv').offsetWidth, height: Q('termarea3xdiv').offsetHeight })); }
                    reader.readAsText(Q('dp2key').files[0]);
                }
            }
        }

        // Send the new terminal size to the agent
        function xTermSendResize() {
            xtermResizeTimer = null;
            if ((xterm != null) && (terminal != null) && (terminal.sendCtrlMsg != null)) {
                if (terminal.urlname == 'sshterminalrelay.ashx') {
                    terminal.socket.send(JSON.stringify({ action: 'resize', cols: xterm.cols, rows: xterm.rows, width: Q('termarea3xdiv').offsetWidth, height: Q('termarea3xdiv').offsetHeight }));
                } else {
                    terminal.sendCtrlMsg(JSON.stringify({ ctrlChannel: '102938', type: 'termsize', cols: xterm.cols, rows: xterm.rows }));
                }
            }
        }

        function connectTerminal(e, contype, options) {
            p12clearConsoleMsg();
            if (!terminal) {
                // Terminal setup
                var termoptions = { protocol: ((options != null) && (typeof options.protocol == 'number')) ? options.protocol : 1 };
                if (options && options.requireLogin) { termoptions.requireLogin = true; }

                /*
                if ([1, 2, 3, 4, 21, 22].indexOf(currentNode.agent.id) == -1) {
                    if (Q('termSizeList').value == 1) { termoptions.cols = 80; termoptions.rows = 25; termoptions.xterm = true; }
                    else if (Q('termSizeList').value == 2) { termoptions.cols = 100; termoptions.rows = 30; termoptions.xterm = true; }
                    else if (Q('termSizeList').value == 3) {
                        // TODO: Try to improve terminal auto-size.
                        termoptions.cols = Math.floor((Q('column_l').clientWidth - 60) / 10);
                        termoptions.rows = Math.floor((Q('column_l').clientHeight - 120) / 20);
                        termoptions.xterm = true;
                    }
                }

                // If shift is pressed
                if ((e && (e.shiftKey == true))) {
                    if (currentNode.agent.id > 4) {
                        if (termoptions.protocol == 1) { termoptions.protocol = 7; } // Switch to user shell
                    } else {
                        if (termoptions.protocol == 1) { termoptions.protocol = 6; } // Switch to Powershell
                    }
                }
                */         
                // If the server requires a shell type
                if ((serverinfo.linuxshell) != null && (currentNode.agent.id > 4)) {
                    if (serverinfo.linuxshell == 'root') { termoptions.protocol = 1; delete termoptions.requireLogin; }
                    if (serverinfo.linuxshell == 'user') { termoptions.protocol = 8; delete termoptions.requireLogin; }
                    if (serverinfo.linuxshell == 'login') { termoptions.protocol = 1; termoptions.requireLogin = true; }
                }

                // Setup a mesh agent xterm terminal
                QV('termarea3xdiv', true);

                // Setup the terminal with auto-fit
                if (xterm != null) { xterm.dispose(); }
                xterm = new Terminal();
                xtermfit = new FitAddon.FitAddon();
                if (xtermfit) { xterm.loadAddon(xtermfit); }
                xterm.setOption('scrollback', 0);
                //xterm.setOption('fontSize', 15);
                xterm.open(Q('termarea3xdiv'));
                xterm.onData(function (data) { if (terminal.urlname == 'sshterminalrelay.ashx') { terminal.socket.send('~' + data); } else { terminal.sendText(data); } })
                if (xtermfit) { xtermfit.fit(); }
                xterm.onResize(function (size) {
                    // Despam resize
                    if (xtermResizeTimer) clearTimeout(xtermResizeTimer);
                    xtermResizeTimer = setTimeout(xTermSendResize, 200);
                });

                // Remove terminal textarea and scrollbar.
                document.getElementsByClassName('xterm-helper-textarea')[0].onfocus = () => { xterm.blur(); if (!fullscreen) toggleKeyboard(); };
                document.getElementsByClassName('xterm-viewport')[0].style.overflow = 'hidden';

                // Setup a terminal tunnel to the agent
                terminal = CreateAgentRedirect(null, CreateRemoteTunnel(tunnelUpdate, options), serverPublicNamePort, authCookie, null, domainUrl);
                terminal.debugmode = debugmode;
                terminal.m.debugmode = debugmode;
                terminal.options = termoptions;
                terminal.options = { cols: xterm.cols, rows: xterm.rows };
                if (termoptions.requireLogin) { terminal.options.requireLogin = true; }
                terminal.Start(null);
                terminal.onStateChanged = onTerminalStateChange;
                terminal.contype = 1;
                terminal.attemptWebRTC = false; // Never do WebRTC on terminal, because of a race condition we can't do it.
                terminal.onConsoleMessageChange = function () { p12setConsoleMsg(terminal.consoleMessage ? formatAgentConsoleMessage(terminal.consoleMessage, terminal.consoleMessageId, terminal.consoleMessageArgs) : null, terminal.consoleMessageTimeout); }
            } else {
                terminal.Stop();
                terminal = null;
                if (fullscreen) { deskToggleFull(); }
            }
            Q('connectbutton2').blur(); // Deselect the connect button so the button does not get key presses.
        }

        //
        // Terminal Shortcut Keys
        //

        function updateTermShortcutKeys() {
            var x = '';
            for (var i = 64; i <= 95; i++) { x += '<div class="menuButton" style="width:70px" onclick="termMenuButton(' + i + ')">' + "Ctrl + " + String.fromCharCode(i) + '</div>'; }
            QH('termButtonMenu', x);
        }

        function termMenuButton(c) {
            toggleMenu(true);
            if (terminal.urlname == 'sshterminalrelay.ashx') {
                // SSH
                terminal.socket.send('~' + String.fromCharCode(c - 64));
            } else {
                // Agent
                terminal.sendText(String.fromCharCode(c - 64));
            }
        }


        //
        // FILES
        //

        function setupFiles() {
            // Setup the files tab
            QE('p13Connect', true);
            p13setActions();
        }

        function onFilesStateChange(xfiles, state) {
            setSessionActivity();
            p13Connect.value = (state == 0) ? "Connect" : "Disconnect";
            var str = StatusStrs[state];
            if (files.webRtcActive == true) { str += ", WebRTC"; }
            Q('p13Status').textContent = str;
            switch (state) {
                case 0:
                    // Disconnected, clear the files
                    QH('p13files', '');
                    p13filetree = null;
                    p13filetreelocation = [];
                    QH('p13currentpath', '');
                    QE('p13FolderUp', false);
                    p13setActions();
                    if (files != null) { files.Stop(); files = null; }
                    if (uploadFile != null) { p13uploadFileTransferDone(); uploadFile = null; }
                    break;
                case 3:
                    p13filetreelocation = [];
                    p13targetpath = '';
                    if (files) {
                        p13filetreelocation = p13targetpath.split('/');
                        files.sendText({ action: 'ls', reqid: 1, path: p13targetpath });
                        //if (files.serverIsRecording == true) { QV('filesRecordIcon', true); }
                    }
                    break;
                default:
                    //console.log('Unknown onFilesStateChange state', state);
                    break;
            }
        }

        function CreateRemoteFiles(onFileUpdate) {
            var obj = { protocol: 5 };
            obj.onFileUpdate = onFileUpdate;
            obj.xxStateChange = function (state) { }
            obj.ProcessData = function (data) { obj.onFileUpdate(data); }
            return obj;
        }

        // Debug Only
        var autoConnectFilesTimer = null;
        function autoConnectFiles(e) { if (autoConnectFilesTimer == null) { autoConnectFilesTimer = setInterval(connectFiles, 100); } else { clearInterval(autoConnectFilesTimer); autoConnectFilesTimer = null; } }

        function connectFiles(e) {
            p13clearConsoleMsg();
            if (!files) {
                // Setup a mesh agent files
                files = CreateAgentRedirect(null, CreateRemoteFiles(p13gotFiles), serverPublicNamePort, authCookie, null, domainUrl);
                files.attemptWebRTC = attemptWebRTC;
                files.onStateChanged = onFilesStateChange;
                files.onConsoleMessageChange = function () {
                    if (files.consoleMessage) {
                        Q('p13FilesConsoleMsg').innerHTML += formatAgentConsoleMessage(files.consoleMessage, files.consoleMessageId, files.consoleMessageArgs);
                        QV('p13FilesConsoleMsg', true);
                        if (p13FilesConsoleMsgTimer != null) { clearTimeout(p13FilesConsoleMsgTimer); }
                        if (files.consoleMessageTimeout) { p13FilesConsoleMsgTimer = setTimeout(p13clearConsoleMsg, files.consoleMessageTimeout * 1000); }
                    } else {
                        p13clearConsoleMsg();
                    }
                }
                files.Start(null);
            } else {
                //QH('Term', '');
                files.Stop();
                files = null;
            }
            p13clipboard = p13clipboardFolder = null;
            p13clipboardCut = 0;
            p13updateClipview();
        }

        var p13filetree = null;
        var p13targetpath = null;
        var p13filetreelocation = [];

        function p13gotFiles(data) {
            if ((data.length > 0) && (data.charCodeAt(0) != 123)) { p13gotDownloadBinaryData(data); return; } // This is ok because 4 first bytes is a control value.
            //console.log('p13gotFiles', data);
            try { data = JSON.parse(decode_utf8(data)); } catch (ex) { data = JSON.parse(data); }
            if (data.action == 'download') { p13gotDownloadCommand(data); return; }

            // Process any SSH actions
            switch (data.action) {
                case 'sshauth': {
                    var x = '';
                    x += addHtmlValue("Authentication", '<select id=dp2authmethod style=width:150px onchange=sshAuthUpdate(event)><option value=1 selected>' + "Username & Password" + '</option><option value=2>' + "Username and Key" + '</option></select>')
                    x += addHtmlValue("Username", '<input id=dp2user style=width:150px maxlength=64 autocomplete=off onkeyup=sshAuthUpdate(event) />');
                    x += '<div id=d2passauth>';
                    x += addHtmlValue("Password", '<input type=password id=dp2pass style=width:150px maxlength=64 autocomplete=off onkeyup=sshAuthUpdate(event) />');
                    x += '</div><div id=d2keyauth style=display:none>';
                    x += addHtmlValue("Key File", '<input type=file id=dp2key style=width:150px maxlength=64 autocomplete=off onchange=sshAuthUpdate(event) />');
                    x += addHtmlValue("Key Password", '<input type=password id=dp2keypass style=width:150px maxlength=64 autocomplete=off onkeyup=sshAuthUpdate(event) />');
                    x += '</div>';
                    x += '<label><input id=dp2keep type=checkbox>' + "Remember credentials" + '</label>';
                    x += '<div id=d2keyauth2 style=font-size:x-small><br />' + "Key file must be in OpenSSH format." + '</div>';
                    setDialogMode(2, "Authentication", 11, p13sshConnectEx, x, 'ssh');
                    setTimeout(sshAuthUpdate, 50);
                    break;
                }
                case 'autherror': { p13setConsoleMsg("Authentication Error", 5000); return; }
                case 'connectionerror': { p13setConsoleMsg("Connection Error", 5000); return; }
                case 'sessionerror': { p13setConsoleMsg("Session expired", 5000); return; }
                case 'sessiontimeout': { p13setConsoleMsg("Session timeout", 5000); return; }
            }

            // Process file upload commands
            if ((data.action != null) && (data.action.startsWith('upload'))) { p13gotUploadData(data); return; }

            if (data.path != null) {
                if (data.dir == null) {
                    if (p13targetpath != '') { p13folderup(); }
                } else {
                    data.path = data.path.replace(/\//g, '\\');
                    if ((p13filetree != null) && (data.path == p13filetree.path)) {
                        // This is an update to the same folder
                        var checkedNames = p13getCheckedNames();
                        p13filetree = data;
                        p13updateFiles(checkedNames);
                    } else {
                        // Make both paths use the same seperator not start with /
                        var x1 = data.path.replace(/\//g, '\\'), x2 = p13targetpath.replace(/\//g, '\\');
                        while ((x1.length > 0) && (x1[0] == '\\')) { x1 = x1.substring(1); }
                        while ((x2.length > 0) && (x2[0] == '\\')) { x2 = x2.substring(1); }
                        if ((x1 == x2) || ((data.path == '\\') && (p13targetpath == ''))) {
                            // This is a different folder
                            p13filetree = data;
                            p13updateFiles();
                        }
                    }
                }
            }
        }

        function p13sshConnectEx(b) {
            if (b == 0) {
                if (files != null) { connectFiles(); } // Disconnect
            } else {
                if (Q('dp2authmethod').value == 1) {
                    files.socket.send(JSON.stringify({ action: 'sshauth', username: Q('dp2user').value, password: Q('dp2pass').value, keep: Q('dp2keep').checked }));
                } else {
                    var reader = new FileReader(), username = Q('dp2user').value, keypass = Q('dp2keypass').value, keep = Q('dp2keep').checked;
                    reader.onload = function (e) { files.socket.send(JSON.stringify({ action: 'sshauth', username: username, keypass: keypass, key: e.target.result, keep: keep })); }
                    reader.readAsText(Q('dp2key').files[0]);
                }
            }
        }

        function p13getCheckedNames() {
            // Save all existing checked boxes
            var checkedNames = [], checkboxes = document.getElementsByName('fd');
            for (var i = 0; i < checkboxes.length; i++) { if (checkboxes[i].checked) { checkedNames.push(p13filetree.dir[checkboxes[i].value].n) }; }
            return checkedNames;
        }

        function p13updateFiles(checkedNames) {
            var html1 = '', html2 = '', displayPath = '<a style=cursor:pointer;color:black onclick=p13folderup(0)>' + "Root" + '</a>', fullPath = 'Root';

            // Work on parsing the file path
            var x = p13filetree.path.split('\\');
            p13filetreelocation = [];
            for (var i in x) { if (x[i] != '') { p13filetreelocation.push(x[i]); } } // Remove empty spaces
            for (var i in p13filetreelocation) { displayPath += ' / <a style=cursor:pointer;color:black onclick=p13folderup(' + (parseInt(i) + 1) + ')>' + EscapeHtml(p13filetreelocation[i]) + '</a>' } // Setup the path we display
            var newlinkpath = p13filetreelocation.join('/');

            // Sort the files
            var filetreexx = p13sort_files(p13filetree.dir);

            // Display all files and folders at this location
            for (var i in filetreexx) {
                // Figure out the name and shortname
                var f = filetreexx[i], name = f.n, shortname;
                shortname = name;
                if (name.length > 70) { shortname = EscapeHtml(name.substring(0, 70)) + "..."; } else { shortname = EscapeHtml(name); }
                name = EscapeHtml(name);

                // Figure out the size
                var fsize = '';
                if (f.s != null) { fsize = getFileSizeStr(f.s); }

                var h = '';
                if (f.t < 3) {
                    var right = '';
                    h = '<div class=filelist file=999><input file=999 style=float:left name=fd class=fcb type=checkbox onchange=p13setActions() value=\'' + f.nx + '\'>&nbsp;<span style=float:right>' + right + '</span><span><div class=fileIcon' + f.t + '></div><a style=cursor:pointer onclick=p13folderset("' + encodeURIComponent(f.nx) + '")>' + shortname + '</a></span></div>';
                } else {
                    var link = shortname;
                    if (f.s > 0) { link = '<a rel=\"noreferrer noopener\" target=\"_blank\" style=cursor:pointer onclick=\"p13downloadfile(\'' + encodeURIComponent(newlinkpath + '/' + name) + '\',\'' + encodeURIComponent(name) + '\',' + f.s + ')\">' + shortname + '</a>'; }
                    h = '<div class=filelist file=3><input file=3 style=float:left name=fd class=fcb type=checkbox onchange=p13setActions() value=\'' + f.nx + '\'>&nbsp;<span style=float:right;padding-right:4px>' + fsize + '</span><span><div class=fileIcon' + f.t + '></div>' + link + '</span></div>';
                }

                if (f.t < 3) { html1 += h; } else { html2 += h; }
            }

            // Display the files and path
            QH('p13files', html1 + html2);
            QH('p13currentpath', displayPath);
            QE('p13FolderUp', p13filetreelocation.length != 0);

            // Re-check all boxes if needed using names
            if (checkedNames != null) { var checkboxes = document.getElementsByName('fd'); for (var i = 0; i < checkboxes.length; i++) { if (checkedNames.indexOf(p13filetree.dir[checkboxes[i].value].n) >= 0) { checkboxes[i].checked = true; } } }

            // Update the actions buttons
            p13setActions();
        }

        function p13folderset(x) {
            p13targetpath = joinPaths(p13filetree.path, p13filetree.dir[x].n).split('\\').join('/');
            if (files) {
                files.sendText({ action: 'ls', reqid: 1, path: p13targetpath });
            }
        }

        function p13folderup(x) {
            if (x == null) { p13filetreelocation.pop(); } else { while (p13filetreelocation.length > x) { p13filetreelocation.pop(); } }
            p13targetpath = p13filetreelocation.join('/');
            if (files) {
                files.sendText({ action: 'ls', reqid: 1, path: p13targetpath });
            }
        }

        var p13sortorder;
        function p13sort_filename(a, b) { if (a.ln > b.ln) return (1 * p13sortorder); if (a.ln < b.ln) return (-1 * p13sortorder); return 0; }
        function p13sort_timestamp(a, b) { if (a.d > b.d) return (1 * p13sortorder); if (a.d < b.d) return (-1 * p13sortorder); return 0; }
        function p13sort_bysize(a, b) { if (a.s == b.s) return p13sort_filename(a, b); return (((a.s - b.s)) * p13sortorder); }

        function p13sort_files(files) {
            var r = [], sortselection = Q('p13sortdropdown').value;
            for (var i in files) { files[i].nx = i; if (files[i].s == null) { files[i].s = 0; } if (files[i].n == null) { files[i].n = i; } files[i].ln = files[i].n.toLowerCase(); r.push(files[i]); }
            p13sortorder = 1;
            if (sortselection > 3) { p13sortorder = -1; sortselection -= 3; }
            if (sortselection == 1) { r.sort(p13sort_filename); }
            else if (sortselection == 2) { r.sort(p13sort_bysize); }
            else if (sortselection == 3) { r.sort(p13sort_timestamp); }
            return r;
        }

        function p13setActions() {
            var advancedFeatures = true; //((currentNode.agent) && (currentNode.agent.id != 14)); // Reduct file feature on some devices.
            if (p13filetree == null) {
                QE('p13DeleteFileButton', false);
                QE('p13NewFolderButton', false);
                QE('p13UploadButton', false);
                QE('p13RenameFileButton', false);
                QE('p13SelectAllButton', false);
                Q('p13SelectAllButton').value = "All";
                QE('p13RefreshButton', false);
                QE('p13CutButton', false);
                QE('p13CopyButton', false);
                QE('p13PasteButton', false);
            } else {
                var cc = p13getFileSelCount(), tc = p13getFileCount(), sfc = p13getFileSelCount(false); // In order: number of entires selected, number of total entries, number of selected entires that are files (not folders)
                var winAgent = true; //((currentNode.agent.id > 0) && (currentNode.agent.id < 5)) || (currentNode.agent.id == 14) || (currentNode.agent.id == 34);
                QE('p13DeleteFileButton', advancedFeatures && (cc > 0) && ((p13filetreelocation.length > 0) || (winAgent == false)));
                QE('p13NewFolderButton', advancedFeatures && ((p13filetreelocation.length > 0) || (winAgent == false)));
                QE('p13UploadButton', advancedFeatures && ((p13filetreelocation.length > 0) || (winAgent == false)));
                QE('p13RenameFileButton', advancedFeatures && (cc == 1) && ((p13filetreelocation.length > 0) || (winAgent == false)));
                QE('p13SelectAllButton', tc > 0);
                Q('p13SelectAllButton').value = (cc > 0 ? "None" : "All");
                QE('p13RefreshButton', true);
                //QE('p13FindButton', advancedFeatures && ((p13filetreelocation.length > 0) || (winAgent == false)));
                QE('p13CutButton', advancedFeatures && (cc > 0) && (cc == sfc) && ((p13filetreelocation.length > 0) || (winAgent == false)));
                QE('p13CopyButton', advancedFeatures && (cc > 0) && (cc == sfc) && ((p13filetreelocation.length > 0) || (winAgent == false)));
                //QE('p13ZipButton', advancedFeatures && (cc > 0) && ((p13filetreelocation.length > 0) || (winAgent == false)));
                QE('p13PasteButton', advancedFeatures && ((p13filetreelocation.length > 0) || (winAgent == false)) && ((p13clipboard != null) && (p13clipboard.length > 0)));
            }
        }

        function p13getFileSelCount(includeDirs) { var cc = 0; var checkboxes = document.getElementsByName('fd'); for (var i = 0; i < checkboxes.length; i++) { if ((checkboxes[i].checked) && ((includeDirs != false) || (checkboxes[i].attributes.file.value == '3'))) cc++; } return cc; }
        function p13getFileSelDirCount() { var cc = 0, checkboxes = document.getElementsByName('fd'); for (var i = 0; i < checkboxes.length; i++) { if ((checkboxes[i].checked) && (checkboxes[i].attributes.file.value == '999')) cc++; } return cc; }
        function p13getFileCount() { var cc = 0; var checkboxes = document.getElementsByName('fd'); return checkboxes.length; }
        function p13selectallfile() { var nv = (p13getFileSelCount() == 0), checkboxes = document.getElementsByName('fd'); for (var i = 0; i < checkboxes.length; i++) { checkboxes[i].checked = nv; } p13setActions(); }
        function p13createfolder() { setDialogMode(2, "New Folder", 3, p13createfolderEx, '<input type=text id=p13renameinput maxlength=64 onkeyup=p13fileNameCheck(event) style=width:100% />'); focusTextBox('p13renameinput'); p13fileNameCheck(); }
        function p13createfolderEx() { files.sendText({ action: 'mkdir', reqid: 1, path: p13filetreelocation.join('/') + '/' + Q('p13renameinput').value }); p13folderup(999); }
        function p13deletefile() { var cc = p13getFileSelCount(), rec = (p13getFileSelDirCount() > 0) ? '<br /><br /><label><input type=checkbox id=p13recdeleteinput>' + "Recursive delete" + '</label><br>' : '<input type=checkbox id=p13recdeleteinput style=\'display:none\'>'; setDialogMode(2, "Delete", 3, p13deletefileEx, (cc > 1) ? (format("Delete {0} selected items?", cc) + rec) : ("Delete selected item?" + rec)); }
        function p13deletefileEx() { var delfiles = [], checkboxes = document.getElementsByName('fd'); for (var i = 0; i < checkboxes.length; i++) { if (checkboxes[i].checked) { delfiles.push(p13filetree.dir[checkboxes[i].value].n); } } files.sendText({ action: 'rm', reqid: 1, path: p13filetreelocation.join('/'), delfiles: delfiles, rec: Q('p13recdeleteinput').checked }); p13folderup(999); }
        function p13renamefile() { var renamefile, checkboxes = document.getElementsByName('fd'); for (var i = 0; i < checkboxes.length; i++) { if (checkboxes[i].checked) { renamefile = p13filetree.dir[checkboxes[i].value].n; } } setDialogMode(2, "Rename", 3, p13renamefileEx, '<input type=text id=p13renameinput maxlength=64 onkeyup=p13fileNameCheck(event) style=width:100% value="' + renamefile + '" />', { action: 'rename', path: p13filetreelocation.join('/'), oldname: renamefile }); focusTextBox('p13renameinput'); p13fileNameCheck(); }
        function p13renamefileEx(b, t) { t.newname = Q('p13renameinput').value; files.sendText(t); p13folderup(999); }
        function p13fileNameCheck(e) { var x = isFilenameValid(Q('p13renameinput').value); QE('idx_dlgOkButton', x); if ((x == true) && (e != null) && (e.keyCode == 13)) { dialogclose(1); } }
        function p13uploadFile() { setDialogMode(2, "Upload File", 3, p13uploadFileEx, '<input type=file name=files id=p13uploadinput style=width:100% multiple=multiple onchange="updateUploadDialogOk(\'p13uploadinput\')" />'); updateUploadDialogOk('p13uploadinput'); }
        function p13uploadFileEx() { p13doUploadFiles(Q('p13uploadinput').files); }
        function p13viewfile() {
            var checkboxes = document.getElementsByName('fd');
            for (var i = 0; i < checkboxes.length; i++) {
                if (checkboxes[i].checked) {
                    if (p13filetree.dir[checkboxes[i].value].s <= 204800) {
                        p13downloadfile(encodeURIComponent(p13filetreelocation.join('/') + '/' + p13filetree.dir[checkboxes[i].value].n), encodeURIComponent(p13filetree.dir[checkboxes[i].value].n), p13filetree.dir[checkboxes[i].value].s, 'viewer');
                    } else { messagebox("File Editor", "Only files less than 200k can be edited."); }
                    break;
                }
            }
        }

        var p13clipboard = null, p13clipboardFolder = null, p13clipboardCut = 0;
        function p13copyFile(cut) { var checkboxes = document.getElementsByName('fd'); p13clipboard = []; p13clipboardCut = cut, p13clipboardFolder = p13targetpath; for (var i = 0; i < checkboxes.length; i++) { if ((checkboxes[i].checked) && (checkboxes[i].attributes.file.value == '3')) { p13clipboard.push(p13filetree.dir[checkboxes[i].value].n); } } p13updateClipview(); }
        function p13pasteFile() {
            var x = '';
            if ((p13clipboard != null) && (p13clipboard.length > 0)) {
                if (p13clipboardCut == 0) {
                    if (p13clipboard.length > 1) { x = format("Confirm copy of {0} entries's to this location?", p13clipboard.length); } else { x = format("Confirm copy of 1 entrie to this location?"); }
                } else {
                    if (p13clipboard.length > 1) { x = format("Confirm move of {0} entries's to this location?", p13clipboard.length); } else { x = format("Confirm move of 1 entrie to this location?"); }
                }
            }
            setDialogMode(2, "Paste", 3, p13pasteFileEx, x);
        }
        function p13pasteFileEx() { files.sendText({ action: (p13clipboardCut == 0 ? 'copy' : 'move'), reqid: 1, scpath: p13clipboardFolder, dspath: p13targetpath, names: p13clipboard }); p13folderup(999); if (p13clipboardCut == 1) { p13clipboard = null, p13clipboardFolder = null, p13clipboardCut = 0; p13updateClipview(); } }
        function p13updateClipview() {
            var x = '';
            if ((p13clipboard != null) && (p13clipboard.length > 0)) {
                if (p13clipboardCut == 0) {
                    if (p13clipboard.length > 1) {
                        x = format("Holding {0} entries for copy" + ', <a href=# onclick="return p13clearClip()" style=cursor:pointer>' + "Clear" + '</a>.', p13clipboard.length);
                    } else {
                        x = format("Holding 1 entrie for copy" + ', <a href=# onclick="return p13clearClip()" style=cursor:pointer>' + "Clear" + '</a>.');
                    }
                } else {
                    if (p13clipboard.length > 1) {
                        x = format("Holding {0} entries for move" + ', <a href=# onclick="return p13clearClip()" style=cursor:pointer>' + "Clear" + '</a>.', p13clipboard.length);
                    } else {
                        x = format("Holding 1 entrie for move" + ', <a href=# onclick="return p13clearClip()" style=cursor:pointer>' + "Clear" + '</a>.');
                    }
                }
            }
            QH('p13bottomstatus', x);
            p13setActions();
        }
        function p13clearClip() { p13clipboard = null; p13clipboardFolder = null; p13clipboardCut = 0; p13updateClipview(); return false; } function updateUploadDialogOk(x) { QE('idx_dlgOkButton', Q(x).value != ''); }
        function getFileSelCount(includeDirs) { var cc = 0; var checkboxes = document.getElementsByName('fc'); for (var i = 0; i < checkboxes.length; i++) { if ((checkboxes[i].checked) && ((includeDirs != false) || (checkboxes[i].attributes.file.value == "3"))) cc++; } return cc; }
        function getFileCount() { var cc = 0; var checkboxes = document.getElementsByName('fc'); return checkboxes.length; }

        //
        // FILES DOWNLOAD
        //

        var downloadFile; // Global state for file download

        // Called by the html page to start a download, arguments are: path, file name and file size.
        function p13downloadfile(x, y, z) {
            if (xxdialogMode || downloadFile || !files) return;
            downloadFile = { path: decodeURIComponent(x), file: decodeURIComponent(y), size: z, tsize: 0, data: '', state: 0, id: Math.random() }
            //console.log('p13downloadFileCancel', downloadFile);
            files.sendText({ action: 'download', sub: 'start', id: downloadFile.id, path: downloadFile.path });
            setDialogMode(2, "Download File", 10, p13downloadFileCancel, '<div>' + downloadFile.file + '</div><br /><progress id=d2progressBar style=width:100% value=0 max=' + z + ' />');
        }

        // Called by the html page to cancel the download
        function p13downloadFileCancel() { setDialogMode(0); files.sendText({ action: 'download', sub: 'cancel', id: downloadFile.id }); downloadFile = null; }

        // Called by the transport when download control command is received
        function p13gotDownloadCommand(cmd) {
            //console.log('p13gotDownloadCommand', cmd);
            if ((downloadFile == null) || (cmd.id != downloadFile.id)) return;
            if (cmd.sub == 'start') { downloadFile.state = 1; files.sendText({ action: 'download', sub: 'startack', id: downloadFile.id }); }
            else if (cmd.sub == 'cancel') { downloadFile = null; setDialogMode(0); }
        }

        // Called by the transport when binary data is received
        function p13gotDownloadBinaryData(data) {
            if (!downloadFile || downloadFile.state == 0) return;
            if (data.length > 4) {
                downloadFile.tsize += (data.length - 4); // Add to the total bytes received
                downloadFile.data += data.substring(4); // Append the data
                Q('d2progressBar').value = downloadFile.tsize; // Change the progress bar
            }
            if ((ReadInt(data, 0) & 1) != 0) { // Check end flag
                saveAs(data2blob(downloadFile.data), downloadFile.file); downloadFile = null; setDialogMode(0); // Save the file
            } else {
                files.sendText({ action: 'download', sub: 'ack', id: downloadFile.id }); // Send the ACK
            }
        }


        //
        // FILES UPLOAD
        //

        var uploadFile;
        function p13doUploadFiles(files) {
            if (xxdialogMode) return;

            // Check if we are going to overwrite any files
            var winAgent = true; //((currentNode.agent.id > 0) && (currentNode.agent.id < 5)) || (currentNode.agent.id == 14) || (currentNode.agent.id == 34);
            var targetFiles = [], overWriteCount = 0;
            for (var i in p13filetree.dir) { if (winAgent) { targetFiles.push(p13filetree.dir[i].n.toLowerCase()); } else { targetFiles.push(p13filetree.dir[i].n); } }
            for (var i = 0; i < files.length; i++) {
                if (winAgent) {
                    if (targetFiles.indexOf(files[i].name.toLowerCase()) >= 0) { overWriteCount++; }
                } else {
                    if (targetFiles.indexOf(files[i].name) >= 0) { overWriteCount++; }
                }
            }

            if (overWriteCount == 0) {
                // If no overwrite, go ahead with upload
                p13uploadFileContinue(1, files);
            } else {
                // Otherwise, prompt for confirmation
                setDialogMode(2, "Upload File", 3, p13uploadFileContinue, format((overWriteCount == 1) ? "Upload will overwrite 1 file. Continue?" : "Upload will overwrite {0} files. Continue?", overWriteCount), files);
            }
        }

        function p13uploadFileContinue(b, files) {
            uploadFile = {};
            uploadFile.xpath = p13filetreelocation.join('/');
            uploadFile.xfiles = files;
            uploadFile.xfilePtr = -1;
            setDialogMode(2, "Upload File", 10, p13uploadFileCancel, '<div id=p13dfileName>' + "Connecting..." + '</div><br /><progress id=d2progressBar style=width:100% value=0 max=0 />');
            p13uploadNextFile();
        }

        // Perform SHA-384 hashing
        const byteToHex = [];
        for (var n = 0; n <= 0xff; ++n) { var hexOctet = n.toString(16).padStart(2, '0'); byteToHex.push(hexOctet); }
        function arrayBufferToHex(arrayBuffer) { return Array.prototype.map.call(new Uint8Array(arrayBuffer), n => byteToHex[n]).join(''); }
        function performHash(data, f) { window.crypto.subtle.digest('SHA-384', data).then(function (v) { f(arrayBufferToHex(v)); }, function () { f(null); }); }
        function performHashOnFile(file, f) {
            // TODO: At some point, try to make this work for files of unlimited size using a digest stream
            var reader = new FileReader();
            reader.onerror = function (err) { f(null); }
            reader.onload = function () { window.crypto.subtle.digest('SHA-384', reader.result).then(function (v) { f(arrayBufferToHex(v)); }, function () { f(null); }); };
            reader.readAsArrayBuffer(file);
        }

        // Push the next file
        function p13uploadNextFile() {
            uploadFile.xfilePtr++;
            if (uploadFile.xfiles.length > uploadFile.xfilePtr) {
                uploadFile.xptr = 0;
                var file = uploadFile.xfiles[uploadFile.xfilePtr];
                QH('p13dfileName', file.name);
                Q('d2progressBar').max = file.size;
                Q('d2progressBar').value = 0;
                if (file.xdata == null) {
                    uploadFile.xfile = file;
                    // If the remote file already exists and is smaller then our file, see if we can resume the trasfer
                    var f = null;
                    for (var i in p13filetree.dir) { if (p13filetree.dir[i].n == file.name) { f = p13filetree.dir[i]; } }
                    if ((f != null) && (f.s <= uploadFile.xfile.size)) {
                        performHashOnFile(uploadFile.xfile, function (hash) { files.sendText(JSON.stringify({ action: 'uploadhash', reqid: uploadFile.xfilePtr, path: uploadFile.xpath, name: file.name, tag: { h: hash.toUpperCase(), s: f.s, skip: f.s == uploadFile.xfile.size } })); });
                    } else {
                        files.sendText(JSON.stringify({ action: 'upload', reqid: uploadFile.xfilePtr, path: uploadFile.xpath, name: file.name, size: uploadFile.xfile.size }));
                    }
                } else {
                    // Data already loaded
                    uploadFile.xdata = file.xdata;
                    files.sendText(JSON.stringify({ action: 'upload', reqid: uploadFile.xfilePtr, path: uploadFile.xpath, name: file.name, size: uploadFile.xdata.byteLength }));
                }
            } else {
                p13uploadFileTransferDone();
            }
        }

        // Used to cancel the entire transfer.
        function p13uploadFileCancel(button, tag) {
            if (uploadFile != null) { files.sendText(JSON.stringify({ action: 'uploadcancel', reqid: uploadFile.xfilePtr })); uploadFile = null; }
            p13uploadFileTransferDone();
        }

        // Used to cancel the entire transfer.
        function p13uploadFileTransferDone() {
            uploadFile = null; // No more files to upload, clean up.
            setDialogMode(0); // Close the dialog box
            p13folderup(9999); // Refresh the current folder
        }

        // Receive upload ack from the mesh agent, use this to keep sending more data
        function p13gotUploadData(cmd) {
            if ((uploadFile == null) || (parseInt(uploadFile.xfilePtr) != parseInt(cmd.reqid))) { return; }
            switch (cmd.action) {
                case 'uploadstart': { uploadFile.xdataPriming = 8; p13uploadNextPart(false); break; } // Send 8 more blocks of 16k to fill the websocket.
                case 'uploadack': { p13uploadNextPart(false); break; }
                case 'uploaddone': { if (uploadFile.xfiles.length > uploadFile.xfilePtr + 1) { p13uploadNextFile(); } else { p13uploadFileTransferDone(); } break; }
                case 'uploaderror': { p13uploadFileCancel(); break; }
                case 'uploadhash': {
                    var file = uploadFile.xfiles[uploadFile.xfilePtr];
                    if (file) {
                        if (cmd.tag.h === cmd.hash) {
                            if (cmd.tag.skip) {
                                p13uploadNextFile();
                            } else {
                                uploadFile.xptr = cmd.tag.s;
                                files.sendText(JSON.stringify({ action: 'upload', reqid: uploadFile.xfilePtr, path: uploadFile.xpath, name: file.name, size: uploadFile.xfile.size, append: true }));
                            }
                        } else {
                            files.sendText(JSON.stringify({ action: 'upload', reqid: uploadFile.xfilePtr, path: uploadFile.xpath, name: file.name, size: uploadFile.xfile.size, append: false }));
                        }
                    }
                    break;
                }
            }
        }

        // Push the next part of the file into the websocket. If dataPriming is true, push more data only if it's not the last block of the file.
        function p13uploadNextPart(dataPriming) {
            if (uploadFile.xreader != null) return; // Data reading already in process
            if (uploadFile.xptr >= uploadFile.xfile.size) return;
            var end = uploadFile.xptr + 65536;
            if (end > uploadFile.xfile.size) { if (dataPriming == true) { return; } end = uploadFile.xfile.size; }
            uploadFile.xreader = new FileReader();
            uploadFile.xreader.onerror = function (err) { console.log(err); }
            uploadFile.xreader.onload = function () {
                var data = uploadFile.xreader.result;
                delete uploadFile.xreader;
                if (data == null) return;
                var dataslice = new Uint8Array(data)
                if ((dataslice[0] == 123) || (dataslice[0] == 0)) {
                    var datapart = new Uint8Array(data.byteLength + 1);
                    datapart.set(dataslice, 1); // Add a zero char at the start of the send, this will indicate that it's not a JSON command.
                    files.send(datapart);
                } else {
                    files.send(dataslice); // The data does not start with 0 or 123 "{" so it can't be confused for JSON.
                }
                uploadFile.xptr = end;
                Q('d2progressBar').value = end;
                if (uploadFile.xptr >= uploadFile.xfile.size) {
                    files.sendText(JSON.stringify({ action: 'uploaddone', reqid: uploadFile.xfilePtr }));
                } else {
                    if (uploadFile.xdataPriming > 0) { uploadFile.xdataPriming--; p13uploadNextPart(true); }
                }
            };
            uploadFile.xreader.readAsArrayBuffer(uploadFile.xfile.slice(uploadFile.xptr, end));
        }

        //
        // PANELS
        //

        var xxcurrentView = -1;
        function go(x) {
            setSessionActivity();
            if (xxdialogMode || xxcurrentView == x) return;
            updateFooterMenu();
            setDialogMode(0);
            // Edit this line when adding a new screen
            for (var i = 0; i < 32; i++) { QV('p' + i, i == x); }
            xxcurrentView = x;
        }

        //
        // POPUP DIALOG
        //

        // undefined = Hidden, 1 = Generic Message
        var xxdialogMode;
        var xxdialogFunc;
        var xxdialogButtons;
        var xxdialogTag;

        // Display a dialog box
        // Parameters: Dialog Mode (0 = none), Dialog Title, Buttons (1 = OK, 2 = Cancel, 3 = OK & Cancel), Call back function(0 = Cancel, 1 = OK), Dialog Content (Mode 2 only)
        function setDialogMode(x, y, b, f, c, tag) {
            setSessionActivity();
            xxdialogMode = x;
            xxdialogFunc = f;
            xxdialogButtons = b;
            xxdialogTag = tag;
            QE('idx_dlgOkButton', true);
            QV('idx_dlgOkButton', b & 1);
            QV('idx_dlgCancelButton', b & 2);
            QV('id_dialogclose', (b & 2) || (b & 8));
            QV('idx_dlgDeleteButton', b & 4);
            QV('idx_dlgButtonBar', b & 7);
            if (y) QH('id_dialogtitle', y);
            for (var i = 1; i < 24; i++) { QV('dialog' + i, i == x); } // Edit this line when more dialogs are added
            QV('dialog', x);
            if (c) { if (x == 2) { QH('id_dialogOptions', c); } else { QH('id_dialogMessage', c); } }
        }

        function dialogclose(x) {
            setSessionActivity();
            var f = xxdialogFunc;
            var b = xxdialogButtons;
            var t = xxdialogTag;
            setDialogMode();
            if (((b & 8) || x) && f) f(x, t);
        }


        //
        // Generic Methods
        //

        function getNodeAmtVersion(node) { if ((node == null) || (node.intelamt == null) || (typeof node.intelamt.ver != 'string')) return 0; var verSplit = node.intelamt.ver.split('.'); if (verSplit.length < 2) return 0; return parseInt(verSplit[0]) + (parseInt(verSplit[1]) / 100); }
        function putstore(name, val) { try { if ((typeof (localStorage) === 'undefined') || (localStorage.getItem(name) == val)) return; if (val == null) { localStorage.removeItem(name); } else { localStorage.setItem(name, val); } } catch (e) { } if (name[0] != '_') { var s = {}; for (var i = 0, len = localStorage.length; i < len; ++i) { var k = localStorage.key(i); if (k[0] != '_') { s[k] = localStorage.getItem(k); } } } }
        function getstore(name, val) { try { if (typeof (localStorage) === 'undefined') return val; var v = localStorage.getItem(name); if ((v == null) || (v == null)) return val; return v; } catch (e) { return val; } }
        function center() { if (xtermfit) xtermfit.fit(); QS('dialog').left = ((((getDocWidth() - 300) / 2)) + 'px'); deskAdjust(); }
        function messagebox(t, m) { QH('id_dialogMessage', m); setDialogMode(1, t, 1); }
        function statusbox(t, m) { QH('id_dialogMessage', m); setDialogMode(1, t); }
        function getDocWidth() { if (window.innerWidth) return window.innerWidth; if (document.documentElement && document.documentElement.clientWidth && document.documentElement.clientWidth != 0) return document.documentElement.clientWidth; return document.getElementsByTagName('body')[0].clientWidth; }
        function haltEvent(e) { if (e.preventDefault) e.preventDefault(); if (e.stopPropagation) e.stopPropagation(); return false; }
        function haltReturn(e) { if (e.keyCode == 13) { haltEvent(e); } }
        function validateEmail(v) { var emailReg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; return emailReg.test(v); }
        function reload() { window.location.href = window.location.href; }
        function getNodeFromId(id) { for (var i in nodes) { if (nodes[i]._id == id) return nodes[i]; } return null; }
        function addHtmlValue(t, v) { return '<table><td style=width:120px>' + t + '<td><b>' + v + '</b></table>'; }
        function addHtmlValue2(t, v) { return '<div><div style=display:inline-block;float:right>' + v + '</div><div style=display:inline-block>' + t + '</div></div>'; }
        function addHtmlValue4(t, v) { return '<table style=width:100%><td style=width:120px>' + t + '<td style=text-align:right><b>' + v + '</b></table>'; }
        function addLink(x, f) { return '<a style=cursor:pointer;text-decoration:none onclick=\'' + f + '\'>&diams; ' + x + '</a>'; }
        function addLinkConditional(x, f, c) { if (c) return addLink(x, f); return x; }
        function passwordcheck(p) { var re = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%^&*()]).{8,}/; return re.test(p); }
        function getFileSizeStr(size) { if (typeof size != 'number') { size = 0; } if (size == 1) return "1 byte"; return format('{0} bytes', size); }
        function focusTextBox(x) { setTimeout(function () { Q(x).selectionStart = Q(x).selectionEnd = 65535; Q(x).focus(); }, 0); }
        var isFilenameValid = (function () { var x1 = /^[^\\/:\*\?"<>\|]+$/, x2 = /^\./, x3 = /^(nul|prn|con|lpt[0-9]|com[0-9])(\.|$)/i; return function isFilenameValid(fname) { return x1.test(fname) && !x2.test(fname) && !x3.test(fname) && (fname[0] != '.'); } })();
        function printDate(d) { return d.toLocaleDateString(args.locale); }
        function printTime(d) { return d.toLocaleTimeString(args.locale); }
        function printDateTime(d) { return d.toLocaleString(args.locale); }
        function format(format) { var args = Array.prototype.slice.call(arguments, 1); return format.replace(/{(\d+)}/g, function (match, number) { return typeof args[number] != 'undefined' ? args[number] : match; }); };
        function nobreak(x) { return x.split(' ').join('&nbsp;'); }
        function getUserName(userid) { if (users && users[userid] != null) return users[userid].name; return userid.split('/')[2]; }
        function addDetailItem(title, value, state) { return '<table style=width:100%><td>' + nobreak(title) + '<td style=text-align:right>' + value + '</table>'; }
        function isPrivateIP(a) { return (a.startsWith('10.') || a.startsWith('172.16.') || a.startsWith('192.168.')); }
        function encodeURIComponentEx(txt) { return encodeURIComponent(txt).replace(/'/g, '%27'); };
        function safeNewWindow(url, target) { var newWindow = window.open(url, target, 'noopener,noreferrer'); if (newWindow) { newWindow.opener = null; } }

    </script>
</body>
</html>